gpt4 book ai didi

android - FragmentManager 已经在 commitAllowingStateLoss() 上执行事务

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:01:32 25 4
gpt4 key购买 nike

java.lang.IllegalStateException: FragmentManager is already executing transactions

我已经阅读了所有关于 StackOverflow 的问题,但没有任何帮助。只是想分享我的经验

public void onResume() {
super.onResume()

if(condition) replaceFragment()
}

public void replaceFragment() {
if (fragmentName != null && !this.isDestroyed()) {
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

ft.replace(container_id, FragmentInstantiate());
ft.commitAllowingStateLoss();
}
}

是 commit()/commitAllowingStateLoss() 导致了 IllegalStateException: FragmentManager already executing transactions。不是 commitNow() 或 executePendingTransactions()

最佳答案

问题:问题是在 Fragment.onResume() 方法中 同步 执行 replaceFragment()。

override fun onResume() {
super.onResume()

if(condition) replaceFragment()
}

解决方案

override fun onResume() {
super.onResume()

if(condition) {
Observable.fromCallable{}
.observeOn(AndroidSchedulers.mainThread())
.subscribe { replaceFragment() }
}

或者使用Handler延迟replaceFragment()的执行

 new Handler().post { replaceFragment() };

关于android - FragmentManager 已经在 commitAllowingStateLoss() 上执行事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49529529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com