【问题标题】:FragmentManager is already executing transactions on commitAllowingStateLoss()FragmentManager 已经在 commitAllowingStateLoss() 上执行事务
【发布时间】:2018-03-28 08:24:57
【问题描述】:
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 已经在执行事务。不是 commitNow() 或 executePendingTransactions()

【问题讨论】:

    标签: android android-fragments fragmentmanager


    【解决方案1】:

    问题: 问题是在 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() };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 2020-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多