When back button is pressed and navigation drawer is open, my app closes instead of closing drawer
To close drawer, when back button is pressed, you have to override onBackPressed()
, this way:
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}