I have a material designed navigation drawer created following this tutorial.
When back button is pressed, the app quit. What I want to do is to close the navigation drawer on back pressed. This is the original code:
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
I added this:
public void onBackPressed() {
if (this.drawerFragment.isDrawerOpen(GravityCompat.START)) {
this.drawerFragment.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
However, I got error cannot resolve method isDrawerOpen
and closeDrawer
.
How can I get it to work?
Back navigation is how users move backward through the history of screens they previously visited. All Android devices provide a Back button for this type of navigation, so you should not add a Back button to your app's UI.
The user can view the navigation drawer when the user swipes a finger from the left edge of the activity. They can also find it from the home activity by tapping the app icon in the action bar. The drawer icon is displayed on all top-level destinations that use a DrawerLayout.
The drawer icon is displayed on all top-level destinations that use a DrawerLayout . To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.
I think you shouldn't be using the fragment to open and close the drawer. instead try using the drawer layout.
public void onBackPressed() {
DrawerLayout layout = (DrawerLayout)findViewById(R.id.drawer_layout);
if (layout.isDrawerOpen(GravityCompat.START)) {
layout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With