Can't use onDismiss() when using custom dialog

2020-07-06 03:14发布

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 capture the custom dialog when it closes,using an onDismiss listener, it never reaches it because I used a custom dialog.

This is part of my activity -

    .
    .
    .
       attributes customizeDialog = new attributes(con,position,pick.getLastVisiblePosition());
        customizeDialog.show();

(The attributes being the name of the class that extends the dialog class).

Here is the event listener I set up when the dialog finishes -

    customizeDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            Log.v("LOG_CAT",attributes.selectedIndexes.get(0) + " " + attributes.selectedIndexes.get(1) + " " + attributes.selectedIndexes.get(2) + " " + attributes.selectedIndexes.get(3) + " " + attributes.selectedIndexes.get(5) + " ");
    }

});

I know i'm doing it wrong,I just don't know how to fix it.

I would really appreciate any help with this problem.

Thanks!

6条回答
霸刀☆藐视天下
2楼-- · 2020-07-06 03:38

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);
查看更多
smile是对你的礼貌
3楼-- · 2020-07-06 03:41

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."

查看更多
我想做一个坏孩纸
4楼-- · 2020-07-06 03:56

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) {

    }
}
查看更多
Root(大扎)
5楼-- · 2020-07-06 03:57

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
    }
}
查看更多
▲ chillily
6楼-- · 2020-07-06 03:57

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.

查看更多
迷人小祖宗
7楼-- · 2020-07-06 03:57

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();
    }
}
查看更多
登录 后发表回答