I have an activity with navigation drawer. Below is my code.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(dLayout.isDrawerOpen(GravityCompat.START)==true) {
dLayout.closeDrawers();
}
else
{
doExit();
}
}
return super.onKeyDown(keyCode, event);
}
private void doExit() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
FirstSelection.this);
alertDialog.setPositiveButton("Yes", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
According to this code, when the user presses back button, the navigation drawer is closed if it is opened, else the doExit();
method is called. But in my case, the else condition works fine, but when I press back button when the drawer is open, the complete app closes. I followed this code How to detect if navigation drawer is open?
Any help would be appreciated.