How to handle Back button with in the dialog?

后端 未结 8 2174
无人共我
无人共我 2020-11-30 01:02

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 b

相关标签:
8条回答
  • 2020-11-30 01:38

    This code works:

        Dialog dlg = new Dialog(thisAct, R.style.DialogTheme);
        dlg.setContentView(view);
        dlg.setCancelable(false);
        dlg.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
        dlg.setOnKeyListener((arg0, keyCode, event) -> {
            Timber.d("onKey(%d)", keyCode);
            //{home intercepting
            if ((keyCode == KeyEvent.KEYCODE_HOME)) {
                Timber.i("HOME pressed");
                return true;
            }
    
            return true;
        });
        dlg.show();
    
    0 讨论(0)
  • 2020-11-30 01:44

    Override method onBackPressed() in your own dialog and use it in your code:

    public class MyDialog extends Dialog {
    
        public MyDialog(@NonNull Context context) {
            super(context);
        }
    
        @Override
        public void onBackPressed() {
            // Do what you want
        }
    }
    

    Use:

    MyDialog dlg = new MyDialog(context);
    dlg.show();
    
    0 讨论(0)
提交回复
热议问题