Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture an event when a user dismiss an alert dialog by pressing the backKey in Android

Tags:

android

I need to catch an event when the user presses the back key and try to dismiss the dialog I have a code like this

AlertDialog alertDialog = new AlertDialog.Builder(AppNotification.this).create();
    alertDialog.setTitle("Caution");
    alertDialog.setMessage("Alert");
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        finish();
    } });
    alertDialog.show();
}

Now here i have given the user an option,But suppose if he presses the back key then i need to perform some other action.how to do it?

like image 676
coderslay Avatar asked Feb 15 '12 07:02

coderslay


1 Answers

There is also

alertDialog.setOnDismissListener(dialog -> { /* code goes here */ });

which seems to be handling specifically the dismiss event.

like image 50
Thanasis Kapelonis Avatar answered Oct 12 '22 10:10

Thanasis Kapelonis