How to handle Back button with in the dialog?

后端 未结 8 2255
无人共我
无人共我 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: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();
    

提交回复
热议问题