简体   繁体   中英

Right place to hide or show the ActionBar in fragments

I have in several places in my apps the need to hide or show the action bar, the problem is that I am still not sure where is the best place to hide or show the ActionBar on the fragments.

I have only one activity and several different fragments, on some of them I need to completely hide the ActionBar , I succeeded to do it but I am concerned of the fact that this ActionBar is actually attached to the parent activity (I got some NullPointerException in the past ...).

So my question is, where should I hide or show my ActionBar when a fragment is added, and where should I hide or show it when the same fragments are removed?

For now I am thinking about for example hiding it in OnActivityCreated , and then showing it back in onDetach but I am afraid that the ActionBar would remain hidden for good if anything wrong happens in the fragment (and therefore onDetach is not called).

UPDATE : I had to do this in my code to avoid a null pointer on my fragment's onStart method:

@Override
public void onStart(){
    super.onStart();

    //if the actionbar exists
    if(getActivity().getActionBar() != null){
        getActivity().getActionBar().hide();
    }
}

UPDATE2 :

I got actually this exception when trying to do it with onAttach : requestFeature() must be called before adding content . This error happened during screen rotations, I think the reason is that I am not using setRetainInstance and that the fragment is trying to add content to the main activity when this one is restarting due to the configuration change.

In order to fix that, I put my code in onActivityCreated , I guess I will only put this there for now on, and use on onDetach or onDestroy to display the ActionBar again.

I had a same requirement in one project and I was able to hide/show the ActionBar inside a Fragment without inconvenients by using onAttach and onDestroy methods. Then, I use this to hide the actionbar when the fragment is created:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    getActivity().getActionBar().hide();
}

And this, when it is removed:

@Override
public void onDestroy() {
    super.onDestroy();
    getActivity().getActionBar().show();
}  

In the same methods, I was also able to addFlags and clearFlags to make the fragment in fullscreen mode. It worked well. Let me know if this helps.

Best approach is to Hide when the Fragment is attached to the Activity . Since this the actual place where the Fragment is associated with the Activity

public void onAttach(Activity activity) {}

And, Show it back when the Fragment is detached from the Activity

public void onDetach() {}

Note: onDestroy() will not be called when you retrain the fragment instance , by setting setRetainInstance .

if you use v4 support library this is the only way.

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    ((ActionBarActivity) getActivity()).getSupportActionBar().hide();
}
@Override
public void onDestroy() {
    super.onDestroy();
    ((ActionBarActivity) getActivity()).getSupportActionBar().show();
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM