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?
just use finish(); – V.J. @user1216003 you are on right way. you will do same as back button with setting the flag in intent.
There are three functions for adding Buttons to Android Dialog, setPositiveButton(int textId, DialogInterface.
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.
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.
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; } });
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 } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With