klifa
klifa

Reputation: 681

Interaction between dismiss and cancel alert dialog android

I have already read some answers on similar discussions but none of them address this directly. I have an alert dialog which i want to do some action when it is dismissed. No matter how it was dismissed (pressing back button or touching outside of the dialog).

Which listener should i set? Dimiss listener or cancel lister or both? I want to do a activity finish() so if both methods will run one after another the app might crash.

Also, is there a way to block so the user CANNOT close the dialog (like overriding the dismiss and setting something false).

Thank you

Upvotes: 1

Views: 1535

Answers (2)

Kanak Sony
Kanak Sony

Reputation: 1560

You should add both listeners here. And in both listeners callback you can call your desired action.

And, you can set it non cancelable, see this -

http://developer.android.com/reference/android/app/Dialog.html#setCancelable(boolean)

Upvotes: 1

NullPointerException
NullPointerException

Reputation: 4008

U can have a listener

dialog.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface arg0) {
                // TODO Auto-generated method stub

                       finish();
            }
        });

this will do your job..

and if you want dialog cannot dismiss you have

dialog.setCancelable(false);

Upvotes: 2

Related Questions