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
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!