Show/Hide DrawerLayout depends on current fragment

android, android-actionbar, android-fragments, android-support-library

Solution

You can use this method to lock or unlock the drawer: `DrawerLayout.setDrawerLockMode(...)`. (There are also two other versions of this method to specify a lock mode for specific drawers.) To lock, use `DrawerLayout.LOCK_MODE_LOCKED_CLOSED`; to unlock, use `DrawerLayout.LOCK_MODE_UNLOCKED`.

If you are using the ActionBarDrawerToggle, you need to add some extra code to prevent the drawer from opening when they click the ActionBarDrawerToggle if you've locked the drawer.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // check lock mode before passing to ActionBarDrawerToggle
    // I assume your drawer is on the left; if not, use Gravity.RIGHT
    int lockMode = mDrawer.getDrawerLockMode(Gravity.LEFT);
    if (lockMode == DrawerLayout.LOCK_MODE_UNLOCKED &&
            mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}

Problem

I have an `Activity` class that have a `DrawerLayout`. That layout shows a list in Drawer and let user to switch between `fragments`. In these `Fragments`, there are some URLs and when user clicsk on that, a `WebviewFragment` would be shown. However, I don't want to show the `DrawerLayout` in the `WebViewFragment`. Instead, I would prefer user would being redirected to previous `Fragment`. Is there any way for me to show/hide the `DrawerLayout` depends on what the current `Fragment` is? I try to call `mDrawerLayout.setVisibility(View.GONE`), but it seems that it is not complete. At least the `ActionBar` icon is still the drawer icon.

Original source