Search code examples
androidnavigation-drawerdrawerlayout

How to disable drawer option in specific fragments of an activity


I have an activity with 5 fragments, I used drawer layout in my activity for a drawer, but I want to use(enable) the drawer in only fragment 2 and I want to disable the drawer option in remaining fragments.

Can any one help me how to do this?


Solution

  • Put two methods in the your activity, one to disable the drawer and one to enable it again, like so:

    public void lockDrawer() {
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }
    
    
    public void unlockDrawer() {
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
    }
    

    Then in your fragments onCreateView(...) method put:

    fragmentInteractionListener.lockDrawer();
    

    for the fragments where the drawer should stay shut and for the fragments where the drawer should stay open put:

    fragmentInteractionListener.unlockDrawer();
    

    P.S: for tutorials in how to correctly implement a fragment interaction listener see:

    https://developer.android.com/training/basics/fragments/communicating.html