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

前端 未结 6 2224
抹茶落季
抹茶落季 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:15

    To add dialog inside CustomDialog class:

    public class MessageBoxDialog extends Dialog implements DialogInterface.OnDismissListener
    {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
             ...
             setOnDismissListener(this);
             ...
        }
    
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
    
        }
    }
    
    0 讨论(0)
  • 2021-02-20 06:17

    If you are using custom dialog and can't dismiss it, try below code. It worked for me.

     new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
               dialog.dismiss();
            }
        }, 1500);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-20 06:22

    One thing to remember is that an OnDismissListener is listening for the dismiss of the child processes. The parent of your customer dialog needs the onDismissListener, not the dialog itself.

    "Interface used to allow the creator of a dialog to run some code when the dialog is dismissed."

    0 讨论(0)
  • 2021-02-20 06:26

    And if you want to have some sort of saving inside the dialog, again, you have to use onDicmissListener since for custom dialogs onDismiss is not called by default:

    public class CustomDialog extends Dialog implements DialogInterface.OnDismissListener {
    
        public CustomDialog(Context context) {
            super(context);
            setupLayout(context);
        }
    
        public CustomDialog(Context context, int theme) {
            super(context, theme);
            setupLayout(context);
        }
    
        protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
            setupLayout(context);
        }
    
        private void setupLayout(Context context) {
            this.context = context;
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.custom_dialog);
            WindowManager.LayoutParams params = getWindow().getAttributes();
            params.width = WindowManager.LayoutParams.FILL_PARENT;
            getWindow().setAttributes(params);
    
            setOnDismissListener(this);
    
            loadPreferences();
        }
    
        private void loadPreferences() {
          // ...
        }
    
        private void savePreferences() {
           // ...
        }
    
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            savePreferences();
        }
    }
    
    0 讨论(0)
  • 2021-02-20 06:32

    I tend to have my activity implement listeners like this...

    public class MyActivity extends Activity
        implements DialogInterface.OnDismissListener {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            attributes customizeDialog = new attributes(con,position,pick.getLastVisiblePosition());
            customizeDialog.setOnDismissListener(this);
            customizeDialog.show();
        }
    
        @Override
        public void onDismiss(DialogInterface dialog) {
            // Do whatever
        }
    }
    
    0 讨论(0)
提交回复
热议问题