Override dialog onBackPressed()?

后端 未结 5 797
醉梦人生
醉梦人生 2020-12-16 09:51

How would I override a Dialog\'s onBackPressed to close the dialog as well as calling finish() on the Activity it is loca

5条回答
  •  攒了一身酷
    2020-12-16 10:35

    I finally added a key listener to my dialog to listen to the Back key. Not as elegant as overriding onBackPressed() but it works. Here is the code:

    dlgDetails = new AlertDialog.Builder(this)
        .setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey (DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && 
                    event.getAction() == KeyEvent.ACTION_UP && 
                    !event.isCanceled()) {
                    dialog.cancel();
                    showDialog(DIALOG_MENU);
                    return true;
                }
                return false;
            }
        })
        //(Rest of the .stuff ...)
    

提交回复
热议问题