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,实现异步。