The back key cannot be captured after Android pops out of Dialog

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.