Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmentManager is already executing transactions on commitAllowingStateLoss()

java.lang.IllegalStateException: FragmentManager is already executing transactions

I've read all StackOverflow questions about that, and nothing helped. Just wanted to share my experience

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();
    }
}

it is commit()/commitAllowingStateLoss() that caused IllegalStateException: FragmentManager is already executing transactions. NOT commitNow() or executePendingTransactions()

like image 977
StepanM Avatar asked Mar 28 '18 08:03

StepanM


1 Answers

PROBLEM: the problem was that in synchronous execution of replaceFragment() in Fragment.onResume() method.

override fun onResume() {
    super.onResume()

    if(condition) replaceFragment()
}

SOLUTION

override fun onResume() {
    super.onResume()

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

or use Handler to postpone execution of replaceFragment()

 new Handler().post { replaceFragment() };
like image 147
StepanM Avatar answered Nov 06 '22 05:11

StepanM