Tofira
Tofira

Reputation: 1634

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

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!

Upvotes: 11

Views: 25764

Answers (6)

Priyanka Thakkar
Priyanka Thakkar

Reputation: 304

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

Upvotes: 1

Amirhossein Ghasemi
Amirhossein Ghasemi

Reputation: 22138

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

    }
}

Upvotes: 0

Mohsen Afshin
Mohsen Afshin

Reputation: 13436

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();
    }
}

Upvotes: 1

Doug
Doug

Reputation: 1

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

Upvotes: 0

Squonk
Squonk

Reputation: 48871

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
    }
}

Upvotes: 19

TREVOR
TREVOR

Reputation: 151

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.

Upvotes: 5

Related Questions