How to hide action bar for fragment?

前端 未结 9 1181
挽巷
挽巷 2021-01-31 08:17

How can I hide action bar for certain fragment? I have searched for the answer at stackoverflow, but I have only found a solution, which involves disabling action bar for main a

相关标签:
9条回答
  • 2021-01-31 08:47

    This solution is for complex non-AppCompat applications that use native ToolBar when running Lollipop onwards and native ActionBar otherwise.

    It assumes you want to hide the ActionBar whenever Fragments are visible.

    Inside onCreate() in each of your Activities:

    getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() 
        {
            @Override
            public void onBackStackChanged() {
                U.ABkk(this, getFragmentManager().getBackStackEntryCount());
            }
        }
    );
    

    OR (much better) inside a 'singleton' class that implements Application.ActivityLifecycleCallbacks

    @Override
    public void onActivityCreated(final Activity A, Bundle savedInstanceState) {
        A.getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                U.ABkk(A, A.getFragmentManager().getBackStackEntryCount());
            }
        });
    }
    

    Inside the utility class:

    /** Show/hide ActionBar for  KitKat devices */
    public static void ABkk(Activity A, int count) {
        if (lollipop()) return;     // No problem when using Toolbar
        ActionBar ab = A.getActionBar();
        if (ab==null) return;
        if (count==1) { ab.hide(); }
        if (count==0) { ab.show(); }
    }
    
    /** Return true if API 21 or greater */
    private static boolean lollipop() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
    }   
    

    Using onActivityCreated() is a solution that requires no changes to your Fragments or Activities!

    0 讨论(0)
  • 2021-01-31 08:49

    If you are using AppCompatActivity (you should) then this is the solution that worked for me:

    ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
    

    You can add this in onCreate(). The support fragment's getActivity() returns a FragmentActivity, and this does not contain the getSupportActionBar() method. Using just getActionBar() gives null-pointer exception if you have AppCompatActivity.

    0 讨论(0)
  • 2021-01-31 08:51

    As it was already mentioned, actionbar may be hidden by (requireActivity() as AppCompatActivity).supportActionBar?.hide() call. If you want to show it in some of your fragments and to hide it in some other fragments, it may be convenient to apply default (for your case) visibility in onViewCreated of your Base fragment:

    abstract class BaseFragment : Fragment() {
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            (requireActivity() as AppCompatActivity).supportActionBar?.show()
        }
    }
    

    and to hide it in particular fragments:

    class HiddenActionBarFragment : BaseFragment() {
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            (requireActivity() as AppCompatActivity).supportActionBar?.hide()
        }
    }
    

    This solution is more flexible than using onStart - onStop for visibility change because during the transition onStart of a different fragment will be called earlier than onStop of the current fragment. So the next fragment won't be able to 'override' actionar visibility applied in onStop of the current fragment.

    0 讨论(0)
  • 2021-01-31 08:57

    getActionBar().hide() or getSupportActionBar().hide() (if using ActionBarCompat v7 lib). Regards

    0 讨论(0)
  • 2021-01-31 09:00

    Put getSupportActionBar().hide() before setContentView in the Activity that holds the fragments.

    Also add this: ((AppCompatActivity) getActivity()).getSupportActionBar().hide() in the fragments before inflating layout. This works if you are using this ActionBarActivity.It also removes my lags in hiding action bar

    0 讨论(0)
  • 2021-01-31 09:01

    Put this code in fragment in which you want to hide toolbar...

     @Override
    public void onResume() {
        super.onResume();
        ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
    }
    @Override
    public void onStop() {
        super.onStop();
        ((AppCompatActivity)getActivity()).getSupportActionBar().show();
    }
    
    0 讨论(0)
提交回复
热议问题