我需要在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()是同样的方法.