Marshal Chen
Marshal Chen

Reputation: 2005

How to know if a dialog is dismissed in Android?

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

Upvotes: 34

Views: 28674

Answers (4)

Sagittarius
Sagittarius

Reputation: 423

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

    }

Upvotes: 0

user1689757
user1689757

Reputation: 475

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.

Upvotes: 2

xemacobra
xemacobra

Reputation: 1954

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

Upvotes: 6

Ken Wolf
Ken Wolf

Reputation: 23279

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)

Upvotes: 84

Related Questions