First, the demand
During the Android development process, the back key cannot be captured after ejecting the dialog, and no response is clicked.
Second, the solution
Cause: When the dialog pops up, the activity loses focus and dialog gets the current focus.
Workaround: In dialog, you can increase the listening of key events for dialog by calling the Setonkeylistener () method.
private void Createdialog () {
Alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.settitle (getString (r.string.acq_select_hint));
Builder.setpositivebutton (GetString (R.string.dialog_cancel), (Dialog, which), Oncancelclicked (dialog));
Builder.setcancelable (FALSE);
Builder.setonkeylistener (Dialog, KeyCode, event), {
if (keycode = = Keyevent.keycode_back) {
oncancelclicked (Dialog);
}
return false;
});
Builder.create (). Show ();
}
private void Oncancelclicked (Dialoginterface dialog) {
Setresult (100);
Finish ();
Dialog.dismiss ();
}
The back key cannot be captured after Android pops out of Dialog