How to hide navigation drawer when opening certain fragment?
Define an interface say DrawerLocker:
interface DrawerLocker{
public void setDrawerLocked(boolean shouldLock);
}
Implement this interface in your activity which contains the NavigationDrawer and override this method.
class YourActivity implements DrawerLocker{
...
...
public void setDrawerLocked(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);
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);
}
});