How to know if a dialog is dismissed in Android?

为君一笑 提交于 2019-11-29 05:11:01

问题


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


回答1:


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)




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/17544819/how-to-know-if-a-dialog-is-dismissed-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!