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()
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() };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With