前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >DialogFragment IllegalStateException: Can not perform this action after onSaveIn

DialogFragment IllegalStateException: Can not perform this action after onSaveIn

原创
作者头像
FangMessi
修改2021-09-28 18:21:07
7670
修改2021-09-28 18:21:07
举报
文章被收录于专栏:码工非码码工非码

一、问题

DialogFragment 偶现IllegalStateException: Can not perform this action after onSaveInstanceState。一般的解决方法,使用commitAllowingStateLoss 替代 commit,但DialogFragment的show方法默认使用的commit,无法修改

二、解决方案

代码语言:txt
复制
public class AllowStateLossDialogFragment extends DialogFragment {
    @Override
    public void show(@NonNull FragmentManager manager, @Nullable String tag) {
        try {
            FragmentManager temp = null;
            // super 里有变量需要赋值
            super.show(temp, tag);
        } catch (Exception e) {
        }

        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitAllowingStateLoss();
    }
}

重写父类show方法,传入空的FragmentManager,目的是触发父类show方法里需要赋值的变量被赋值,然后在try-catch之后手动调用commitAllowingStateLoss

三、结语

  1. 送人玫瑰 手留余香
  2. 如果对您有帮忙,请点赞支持我~~

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、问题
  • 二、解决方案
  • 三、结语
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档