DialogFragment Close Event

大兔子大兔子 提交于 2019-12-05 02:17:12

Override onDismiss() in your DialogFragment, or use setOnDismissListener() in the code block where you are building the fragment.

Krzysiek

I faced similar problem, but I wanted to inform another activity about the dialog dismiss (not the activity that created and showed the dialog).

Although you can just override the onDismiss() method in your DialogFragment as Austyn Mahoney suggested, yet you can NOT use setOnDismissListener(), because DialogFragment simply does not provide such method (according to: Android Developers DialogFragment Reference).

But still there is another nice way to inform any other activity about the dialog dismiss, (I've found it there: DialogFragment and onDismiss), here it comes:

Firstly you should make your Activity (the one that you want to pass information about dialog dismiss) implement OnDismissListener:

public final class YourActivity extends Activity implements DialogInterface.OnDismissListener {

    @Override
    public void onDismiss(final DialogInterface dialog) {
        //Fragment dialog had been dismissed
    }

}

Again according to Android Developers DialogFragment Reference DialogFragment already implements OnDismissListener with onDismiss() method. That's why you should override it and call there your onDismiss() method which you implemented in YourActivity:

public final class DialogFragmentImage extends DialogFragment {

    @Override
    public void onDismiss(final DialogInterface dialog) {
        super.onDismiss(dialog);
        final Activity activity = getActivity();
        if (activity instanceof DialogInterface.OnDismissListener) {
            ((DialogInterface.OnDismissListener) activity).onDismiss(dialog);
        }
    }

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