FragmentManager is already executing transactions on commitAllowingStateLoss()
Asked Answered
Q

1

5
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()

Quietude answered 28/3, 2018 at 8:24 Comment(0)
Q
11

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() };
Quietude answered 28/3, 2018 at 8:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.