DialogFragment调用show()报Can not perform this action after onSaveInstanceState的解决办法
参考文章:
- 使用自定义DialogFragment出现IllegalStateException: Can not perform this action after onSaveInstanceState异常
- Fragment Or DialogFragment Can not perform this action after onSaveInstanceState
解决办法:
- 在BaseDialog中重写show()和dismiss()方法。
@Override
public void show(FragmentManager manager, String tag) {
// super.show(manager, tag);
try {
Class c = Class.forName("android.support.v4.app.DialogFragment");
Constructor con = c.getConstructor();
Object obj = con.newInstance();
Field dismissed = c.getDeclaredField("mDismissed");
dismissed.setAccessible(true);
dismissed.set(obj, false);
Field shownByMe = c.getDeclaredField("mShownByMe");
shownByMe.setAccessible(true);
shownByMe.set(obj, false);
} catch (Exception e) {
e.printStackTrace();
}
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commitAllowingStateLoss();
}
@Override
public void dismiss() {
// super.dismiss();
dismissAllowingStateLoss();
}
当DialogFragment在onSaveInstanceState之后调用show()时,会出现IllegalStateException。解决这个问题的方法是在自定义DialogFragment中重写show()和dismiss()方法,避免在不适当的状态下显示或关闭DialogFragment。

1万+

被折叠的 条评论
为什么被折叠?



