Can't use onDismiss() when using custom dialogs - Android

前端 未结 6 2329
抹茶落季
抹茶落季 2021-02-20 05:31

I\'m working on a little program, and I need to add a custom dialog that passes some info to the calling acitivity when it closes. I extended the dialog class, and when I try to

6条回答
  •  孤独总比滥情好
    2021-02-20 06:22

    You could have your calling activity implement a custom listener interface that is called when the dialog closes:

    public interface MyDialogListener {
        void OnCloseDialog();
    }
    
    public class MyActivity implements MyDialogListener {
        public void SomeMethod() {
            MyDialog myDialog = new MyDialog(this, this);
            myDialog.show();
        }
    
        public void OnCloseDialog() {
            // Do whatever you want to do on close here
        }
    
    }
    
    public class MyDialog extends Dialog {
        MyDialogListener mListener;
    
        public MyDialog (Context context, MyDialogListener listener) {
            super(context, R.style.Dialog);
            mListener = listener;
        }
    
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.CloseButton:
                    mListener.OnCloseDialog();
                    dismiss()
                    break;
                default:
                    //...
            }
        }
    }
    

    This is especially useful if you want to send stuff back to the caller at any other time besides on dismissal.

提交回复
热议问题