My Android application manages multiple fragments. I am seeing a large number of crashes in the field, however, containing this log line:
java.lang.IllegalStateException: Failure saving state: active XxxFragment{81e598 id=0x7f0b0069 tag_yyy} has cleared index: -1
Searches of answers in Stack Overflow have been fruitless; I seem to have a lot of company in wondering exactly what this exception means. Digging into the exception trace and Android sources, I can see that the exception is coming from the point at which my main Activity is saving its state (FragmentActivity.onSaveInstanceState), and the individual Fragments are being written into a Parcelable. Each Fragment has an index (called mIndex) which must be nonnegative, but it's not really clear from the code why that must be the case, as mIndex is never used again in that function.
I have no good idea how the Fragment gets into this state, or what I can do about it. I have been unable to reproduce the error in my own test environment. Can anyone shed any light on how to avoid and/or deal with this exception?
Related SO questions:
java.lang.IllegalStateException: Failure saving state: active has cleared index in fragment
What does active fragment has cleared index: -1 mean and how do I fix it?