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
解决办法一
在PromptDialogFragment
中重写show()
和dismiss()
方法
@Override
public void show(FragmentManager manager, String tag) {
//super.show(manager, tag);
try {
Class c = Class.forName("androidx.fragment.app.DialogFragment");
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) {
NLog.e(Constants.TAG, Log.getStackTraceString(e));
}
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commitAllowingStateLoss();
}
@Override
public void dismiss() {
//super.dismiss();
dismissAllowingStateLoss();
}
解决方法二
(1)自建包androidx.fragment.app
,并在包下新建类MyDialogFragment
继承DialogFragment
package androidx.fragment.app;
public class MyDialogFragment extends DialogFragment {
public boolean getDismissed() {
return mDismissed;
}
public void setDismissed(boolean dismissed) {
this.mDismissed = dismissed;
}
public boolean getShownByMe() {
return mShownByMe;
}
public void setShownByMe(boolean shownByMe) {
this.mShownByMe = shownByMe;
}
}
(2)在PromptDialogFragment
中重写show()
和dismiss()
方法
public class LoginPromptDialogFragment extends MyDialogFragment
...
@Override
public void show(FragmentManager manager, String tag) {
setDismissed(false);
setShownByMe(false);
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commitAllowingStateLoss();
}
@Override
public void dismiss() {
//super.dismiss();
dismissAllowingStateLoss();
}
}
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/26/dialogfragment-call-show-and-report-can-not-perform-this-action-after-onsaveinstancestate/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
打赏
海报
DialogFragment调用show()报Can not perform this action after onSaveInstanceState的解决办法
参考文章
使用自定义DialogFragment出现IllegalStateException: Can not perform this action after onSaveInstanceState异常
Fragment Or DialogFragment Ca……
文章目录
关闭
共有 0 条评论