I have an app and When the user presses BACK Button, it shows dialog, the dialog cannot be cancelled so I set it setCancelable(false)
but I want to dismiss the dialog when the user presses BACK button Again. ( if the user presses Back Button, Show dialog. if the user presses Back Button again , dismiss dialog )
My Code below
private boolean dialogshown = false;
private AlertDialog d;
public boolean onKeyDown(int keycode, KeyEvent e) {
if(keycode == KeyEvent.KEYCODE_BACK)
if(!dialogshown) {
d = new AlertDialog.Builder(MainActivity.this).create();
d.setButton(AlertDialog.BUTTON_POSITIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
dialogshown = false;
}
});
d.setButton(AlertDialog.BUTTON_NEGATIVE, "Exit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
System.exit(0);
}
});
d.setCancelable(false);
d.show();
dialogshown = true;
} else {
try{ d.dismiss(); }catch (Exception exc){Toast.makeText(this,"Error back",Toast.LENGTH_LONG).show();}
}
return super.onKeyDown(keycode, e);
}
The problem is when I press Back button it show a dialog, but when I press it again Nothing Happens
Aucun commentaire:
Enregistrer un commentaire