Show / hide DrawerLayout depends on the current fragment

I have an Activity class that has DrawerLayout . This layout shows the list in Drawer and allows the user to switch between fragments . There are several URLs in these fragments , and when a user clicks on it, WebviewFragment will be displayed. However, I do not want to show DrawerLayout in WebviewFragment . Instead, I would rather have the user redirected to the previous Fragment .

Is there any way to show / hide DrawerLayout depends on the current Fragment ? I am trying to call mDrawerLayout.setVisibility(View.GONE ) but it seems that it is not complete. At least the ActionBar icon is still a box icon.

+6
source share
2 answers

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

If you are using an ActionBarDrawerToggle, you need to add additional code to prevent the box from opening when you click on the ActionBarDrawerToggle if you lock the box.

 @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); } 
+18
source

Why not discover a new activity that only has a WebView instead? Then the user will need to click the "Back" button to return to the Activity using DrawerLayout, and then there will be no problems.

In addition, you do not need to have such an action yourself, you can let Android find a browser to open them using this code:

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("HTTP_URL_GOES_HERE")); startActivity(intent); 
+1
source

Source: https://habr.com/ru/post/951444/


All Articles