简体   繁体   English

隐藏或显示片段中的ActionBar的正确位置

[英]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. 我的应用程序中有多个地方需要隐藏或显示操作栏,但问题是我仍然不确定在片段上隐藏或显示ActionBar的最佳位置。

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 ...). 我只有一个活动和几个不同的片段,其中一些我需要完全隐藏ActionBar ,我成功完成了它,但是我担心这个ActionBar实际上是附加到父活动的事实(我在其中得到了NullPointerException过去 ...)。

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? 所以我的问题是,添加片段时,我应该在哪里隐藏或显示我的ActionBar ;删除相同片段时,我应该在哪里隐藏或显示它?

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). 现在,我正在考虑例如将其隐藏在OnActivityCreated ,然后再将其显示在onDetach但我担心如果片段中发生任何错误(因此不会调用onDetach ), ActionBar将永远保持隐藏状态。

UPDATE : I had to do this in my code to avoid a null pointer on my fragment's onStart method: 更新 :我必须在代码中执行此操作,以避免片段的onStart方法上的空指针:

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

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

UPDATE2 : UPDATE2

I got actually this exception when trying to do it with onAttach : requestFeature() must be called before adding content . 当尝试使用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. 此错误发生在屏幕旋转期间,我认为原因是我没有使用setRetainInstance并且由于配置更改,该片段在重新启动时,该片段正在尝试向主要活动添加内容。

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. 为了解决这个问题,我将代码放在onActivityCreated ,我想现在只将其放在此处,然后使用onDetachonDestroy再次显示ActionBar

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. 我在一个项目中有相同的要求,并且可以使用onAttachonDestroy方法隐藏/显示FragmentActionBar而不会带来任何不便。 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. 在相同的方法中,我还能够添加addFlagsclearFlags以全屏模式制作片段。 It worked well. 运行良好。 Let me know if this helps. 让我知道是否有帮助。

Best approach is to Hide when the Fragment is attached to the Activity . 最好的方法是在Fragment附加到ActivityHide Since this the actual place where the Fragment is associated with the Activity 因为这是FragmentActivity相关联的实际位置

public void onAttach(Activity activity) {}

And, Show it back when the Fragment is detached from the Activity 并且,当FragmentActivity分离时,将其Show回来

public void onDetach() {}

Note: onDestroy() will not be called when you retrain the fragment instance , by setting setRetainInstance . 注意:通过设置setRetainInstance重新训练片段实例时,不会调用onDestroy()

if you use v4 support library this is the only way. 如果使用v4支持库,则这是唯一的方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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