问题
I have 2 Fragments mainFragment and childFragment. I want to show actionbar in mainFragment but wants to hide in childFragment. All is working fine except that when I comes back to mainFragment from childFragment and again goes to childFragment action bar shows for seconds before get hidden ..
I don't know why ? HELP
In childFragment I am doing this
@Override
public void onResume() {
super.onResume();
((ActionBarActivity) getActivity()).getSupportActionBar().hide();
mainActivity.mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
// handle back button's click listener
mainActivity.onBackPressed();
((ActionBarActivity) getActivity()).getSupportActionBar().show();
mainActivity.mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
return true;
}
return false;
}
});
}
回答1:
You should handle all transitions in your main activity... There you should handle all the logic according to your needs - such as hiding/showing the action bar and such...
回答2:
Try to do this in your fragmen's onActivityCreated method:
((ActionBarActivity) getActivity()).getSupportActionBar().hide();
mainActivity.mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
Edit: I assume that you are handling your fragment transactions in your activity. Before you replace your fragment in activity with:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, new YourFragment()).commit();
do your actionbar hide and show operations according to which fragment you are replacing. (main or child)
来源:https://stackoverflow.com/questions/31565364/android-hiding-action-bar-in-fragment