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

前端 未结 6 2322
抹茶落季
抹茶落季 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: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();
        }
    }
    

提交回复
热议问题