7

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.

Bear
  • 5,138
  • 5
  • 50
  • 80

2 Answers2

18

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);
}
Karakuri
  • 38,365
  • 12
  • 84
  • 104
1

Why not open a new Activity that has only a WebView as its content instead? Then the user will have to press the back button to go back to the Activity with the DrawerLayout, and then there will be no problems.

Alternatively, you don't have to have such an activity yourself, you can let Android find a browser for them to open instead using this code:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("HTTP_URL_GOES_HERE"));
startActivity(intent);
Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • 1. Actually, I am not sure when to start a new activity and when to add a fragment to backstack. But I think showing a fragment has less overhead (less loading time coompared to start a new activity) From my understanding, many app has only one MainActivity and many many fragment. 2. Many app has its own WebViewFragment/ WebViewActivity to show a simple web. Because just don't want user to leave the app. – Bear Aug 10 '13 at 04:52
  • Showing a new Activity shouldn't take noticeably longer than making a new Fragment. I think in reality the load time will depend much more on how long it takes the web page to load. If you want to keep the suer in your app, just move the code from your WebView fragment to an Activity and use that. Or see my other answer to stick with your current approach. – Karakuri Aug 10 '13 at 05:03