Skip to content Skip to sidebar Skip to footer

How To Hide Navigation Drawer When Opening Certain Fragment?

I'm pretty new in android development. I want to hide navigation drawer when user is not logged in. So basically, it will shown login fragment (better in full screen like Facebook

Solution 1:

Define an interface say DrawerLocker:

interfaceDrawerLocker{
   publicvoidsetDrawerLocked(boolean shouldLock);
}

Implement this interface in your activity which contains the NavigationDrawer and override this method.

classYourActivityimplementsDrawerLocker{
...
...
publicvoidsetDrawerLocked(boolean enabled){
    if(enabled){
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }else{
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
    }

}

Now in your Fragment's onCreateView, call this method like:

 ((DrawerLocker)getActivity()).setDrawerLocked(true);

In your Fragment's onDestroyView, re enable the drawer layout:

((DrawerLocker)getActivity()).setDrawerLocked(false);

Solution 2:

This code in activity onCreate method will block drawer when you will have more than one fragment in stack:

getSupportFragmentManager().addOnBackStackChangedListener(() -> {
        if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        } else {
            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        }
    });

Post a Comment for "How To Hide Navigation Drawer When Opening Certain Fragment?"