I\'ve got a monkey crash whereby
java.lang.IllegalStateException: Failure saving state: FragmentB has target not in fragment manager: FragmentA
at android.s
We ran into this issue recently. We have implemented a custom adapter extending android.support.v4.app.FragmentStatePagerAdapter. With android.support.v4.app.FragmentManager, we have set up several fragments in the pager, as well as several other fragments outside of the pager. The fragments are managed in a single activity. There are some instances where we were setting the target (with setTargetFragment) of non-paging fragments to fragments that may or may not be contained in the paging adapter. Because FragmentStatePagerAdapter only maintains a certain number of fragments, the fragments that were set as targets and what FragmentStatePagerAdapter deemed no longer needed, were destroyed...and a potentially inconsistent state if the fragments that had those targets still existed. This led to the exception being thrown whenever the app lost focus (either when the screen turned off or the app went into the background), i.e., when onSaveInstanceState was called.
To prevent this exception, in onSaveInstanceState, we checked to see what fragments were currently in the fragment manager. If there were any inconsistencies (i.e., a "target" fragment was missing), we removed the fragment that had that target set. In our case, we only had three fragments where we were setting a target so we knew exactly what to look for.
I don't believe there is any other way to handle this case, but if anyone has any helpful feedback, it would be much appreciated.