DialogFragment Close Event

强颜欢笑 提交于 2020-01-02 01:04:12

问题


I need to handle the end of a DialogFragment (after a call to .dismiss) - for example, I would show a toast inside the activity that "contains" the fragment after dismiss.

How do I handle the event?


回答1:


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




回答2:


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

}


来源:https://stackoverflow.com/questions/15163520/dialogfragment-close-event

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