Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Back button with in the dialog?

I am developing an application that when the button is pressed, it opens a dialog with OK and Cancel buttons.

It works fine.

When the user presses the back button, I am handling this as follows

public boolean onKeyDown(int keyCode, KeyEvent event)  {     if ((keyCode == KeyEvent.KEYCODE_BACK))      {      }  return super.onKeyDown(keyCode, event); } 

But the above method is not called. How can I handle this?

like image 490
kiran Avatar asked Apr 27 '12 06:04

kiran


People also ask

How do you press back button?

just use finish(); – V.J. @user1216003 you are on right way. you will do same as back button with setting the flag in intent.

What are the three buttons that can be added to a dialog?

There are three functions for adding Buttons to Android Dialog, setPositiveButton(int textId, DialogInterface.

How do you prevent a dialog from closing when a button is clicked?

If you wish to prevent a dialog box from closing when one of these buttons is pressed you must replace the common button handler for the actual view of the button.

What are dialog Buttons?

A control to define one or more buttons on a form. It should bring some more flexibility to the forms transformed into dialogs. Author: Diana Birkelbach.


2 Answers

dialog.setOnKeyListener(new Dialog.OnKeyListener() {              @Override             public boolean onKey(DialogInterface arg0, int keyCode,                     KeyEvent event) {                 // TODO Auto-generated method stub                 if (keyCode == KeyEvent.KEYCODE_BACK) {                     finish();                     dialog.dismiss();                 }                 return true;             }         }); 
like image 98
Yasin Hassanien Avatar answered Sep 27 '22 18:09

Yasin Hassanien


Sounds like you want to set the OnCancelListener when you create the Dialog. It looks like this:

dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {              @Override     public void onCancel(DialogInterface dialog) {         //do whatever you want the back key to do     } }); 
like image 35
alexc Avatar answered Sep 27 '22 19:09

alexc