android – 如何在onstop()中安全地解除DialogFragment?

前端之家收集整理的这篇文章主要介绍了android – 如何在onstop()中安全地解除DialogFragment?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在FragmentActivity的onStop()中解除DialogFragment,如果它正在显示,这就是我做的
if(mAlertDlg != null && mAlertDlg.getDialog() != null)
        mAlertDlg.dismiss();

但我通常得到IllegalStateException.那么请告诉我为什么代码错误以及在onStop()中解除DialogFragment的正确方法是什么?谢谢.

解决方法

您应该使用dialogFragment.dismissAllowingStateLoss().正如文档所述的commitAllowingStateLoss():

“Like commit() but allows the commit to be executed after an
activity’s state is saved. This is dangerous because the commit can be
lost if the activity needs to later be restored from its state,so
this should only be used for cases where it is okay for the UI state
to change unexpectedly on the user.”

所以对于dismissAllowingStateLoss()是同样的方法.

猜你在找的Android相关文章