I have a listener in my Activity replace a Fragment after a network request is finished on another thread. So this listener is calling a line of code like this:
getFragmentManager().beginTransaction().replace(R.id.container, fragment, fragmentTag).commit();
The commit() in this line of code occasionally throws an IllegalStateException. According to the docs,
A transaction can only be committed with this method prior to its containing activity saving its state. If the commit is attempted after that point, an exception will be thrown. This is because the state after the commit can be lost if the activity needs to be restored from its state. See commitAllowingStateLoss() for situations where it may be okay to lose the commit.
While looking into this I came across the FragmentManager.isDestroyed() method. The javadocs read:
Returns true if the final Activity.onDestroy() call has been made on the FragmentManager's Activity, so this instance is now dead.
I guess I'm just a bit confused about the implications of the FragmentManager's Activity instance being dead. When should we use FragmentManager.isDestroyed()? Would checking it before committing the replace FragmentTransaction avoid the IllegalStateException?