commitNowAllowingStateLoss导致的FragmentManager is already executing transactions

commitNowAllowingStateLoss导致的FragmentManager is already executing transactions

fragment版本1.3.6

异常日志

Fatal Exception: java.lang.IllegalStateException
FragmentManager is already executing transactions
androidx.fragment.app.FragmentManager.ensureExecReady (FragmentManager.java:4)
androidx.fragment.app.FragmentManager.execSingleAction (FragmentManager.java:11)
androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss (BackStackRecord.java:2)

原因分析

查看源码发现发生这个异常是因为FragmentManager一次操作未结束时又进行了下一次操作。(通过mExecutingActions这个变量进行判定的)

说明有两次Fragment操作很接近,并且因为是commitNowAllowingStateLoss,代码是同步的且所有相关操作都在主线程,不存在多线程。所以一般应该是发生了嵌套行为。即:

mExecutingActions =true;

xxx();//xxx方法中又同步调用了Fragment的操作,导致发现mExecutingActions为true抛出异常

mExecutingActions =false;

查看异常上下文,发现是在“添加Fragment—》Fragment内部初始化相关组件失败—》回调到外部移除该Fragment“,且移除Fragment的方法使用的是runOnUiThread,runOnUiThread里面判定了如果是主线程,则直接运行Runnable,此时是同步代码,发生了上方的同步行为触发该异常。

解决方法

主要思路为通过异步的方式避免这种嵌套调用就行了

方法1

使用commitAllowingStateLoss代替commitNowAllowingStateLoss,因为commitNowAllowingStateLoss是同步,commitAllowingStateLoss是异步。

方法2

使用handler.post或者view.post替代runOnUiThread,实现异步。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值