34

If the dialog is dismissed,I want to do something for my background.So I want to know if the dialog is dismissed

Marshal Chen
  • 1,985
  • 4
  • 24
  • 35
  • If you do not expect to receive any data but just want to know the dialog is gone perhaps this may help: https://gist.github.com/CrandellWS/ac79d3864a96344d204d869d64fd1922 – CrandellWS May 29 '16 at 17:27

4 Answers4

84

You can use an onDismissListener

http://developer.android.com/reference/android/content/DialogInterface.OnDismissListener.html

public Dialog createDialog() {
    Dialog d = new Dialog(this);
    d.setOnDismissListener(new OnDismissListener() {
        @Override
        public void onDismiss(final DialogInterface arg0) {
            // do something
        }
    });
    return d;
}

If you are using a DialogFragment just override onDismiss()

http://developer.android.com/reference/android/app/DialogFragment.html#onDismiss(android.content.DialogInterface)

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • 2
    Fantastic BUT it does not get the case of the device being rotated - which kills the dialog. Any ideas? – Fattie May 25 '14 at 16:56
  • 3
    @JoeBlow When the device is rotated and the activity recreated `onCreate` will be called and `Bundle savedInstanceState` will not be equal to `null`. You can store any state (for example, a boolean whether the dialog was showing before the device was rotated) in `onsaveInstanceState` and reference it here. – Ken Wolf May 25 '14 at 18:06
  • Great answer. Unfortunately `setOnDismissListener` is only supported on API 17+. Is there a way for earlier API versions? – xemacobra Feb 19 '15 at 17:04
  • `setOnDismissListener` has been available since API level 1 I believe. http://developer.android.com/reference/android/app/Dialog.html#setOnDismissListener(android.content.DialogInterface.OnDismissListener) – Ken Wolf Feb 19 '15 at 17:28
  • Weird, Android Studio kept complaining. Thanks though. – xemacobra Feb 19 '15 at 19:42
  • Is there a way I can find out which dialog existed in a clean way (My activity launches different dialogs for different purposes and all are custom dialogs)? – user2148707 Mar 28 '20 at 10:14
6

@Ken Wolf has a great answer to this question.

Just wanted to add that onDismissListener was only introduced in API 17. If you are trying to support something lower, you can use onCancelListener, which is not as good but covers cases like backButton and tapping outside of the AlertDialog.

http://developer.android.com/reference/android/content/DialogInterface.OnCancelListener.html#onCancel(android.content.DialogInterface)

public Dialog createDialog() {
    Dialog d = new Dialog(this);
    d.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            // do something
        }
    });
}
xemacobra
  • 1,954
  • 2
  • 21
  • 21
2

I noticed that the onDismissListener is called even when you select one of the options in the alert (Yes/No/Neutral button). For me onCancelListener was the best option since I needed something that tracked an explicit closing of the dialog by clicking outside the alert area.

user1689757
  • 475
  • 5
  • 19
0

When dialog closed, you can use dialog.setOnDismissListener at the following code with the usage of an updated dialog code.

private void ShowDialog() {      
            View view = LayoutInflater.from(ActivityMain.this).inflate(R.layout.dialog, null);
            dialog = new Dialog(ActivityMain.this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(true);
            dialog.addContentView(view, new RelativeLayout.LayoutParams(
                        WindowManager.LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.WRAP_CONTENT));
            Button dialogBtn = (Button) dialog.findViewById(R.id.button);
            dialogBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();                
                }
            });
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(final DialogInterface arg) {
                    //when dialog closed
                }
            });

    }
Sagittarius
  • 355
  • 3
  • 6