0

After rotating a device, any fragment we have on the backstack or in the fragmentSupportManager, activates the onSaveInstanceState.

However, after the first rotation, everything inside is null (all variables) and the app crashes with null pointer exceptions.

I'm wondering why if the fragment is virtually destroyed to nothing (all values are null) why the fragment still exists and onSaveInstanceState is being called?

My code is quite deep, with multiple base classes, and presenter classes to lift a lot of the code out of the fragments. I'm also only using one Activity and using fragmentTransaction.replace.

I can add code if people require it, but there's a lot! I'm hoping there is something obvious or something that I don't understand about the way the backstack works/retains it's fragments.

links I have looked at that haven't helped are

Will onSaveInstanceState be called on fragments in the back stack?
Once for all, how to correctly save instance state of Fragments in back stack?
Using onSaveInstanceState with fragments in backstack?
BackStack Fragments blank on rotation
Save state of fragments in backstack

Community
  • 1
  • 1
Russ Wheeler
  • 2,590
  • 5
  • 30
  • 57
  • If i understand your question very well, you saved your variable data in onSavedInstance such that when it rotates this data are persisted right? and how do you retrieve the data back? #justAsking – Paul Okeke Jul 29 '15 at 17:26
  • Are you also using [addToBackStack](http://developer.android.com/reference/android/app/FragmentTransaction.html#addToBackStack(java.lang.String)) in the fragmentTransaction? – petey Jul 30 '15 at 15:34
  • petey, we are, but see below and I've found a workable solution – Russ Wheeler Jul 31 '15 at 09:25

1 Answers1

0

I have exactly the same problem. Lets say I have Frag A, calling Frag B When I get from Frag A to Frag B, Frag A exists on backstack and is not showing. While in Frag B, on a first rotate the onSavedInstanceState() is being called for Frag A and it works fine, since the Frag A on back stack exists and all the data was there.. When I rotate the device again in Frag B, the Frag A onSavedInstanceState() is called again for some unknown reason.. between these calls a restore was never issued since onCreateView has not been called since Frag A is not visible. Why it is doing that I have 0 clue.

What I do, is I check if Bundle is empty (and bundle does exist after 2nd rotate, its just empty) and if it is, I try to recreate Frag from scratch, which means reloading fresh data from a request... which is not the best solution..

vankiz
  • 106
  • 1
  • 6
  • 1
    This article describes the exact problem http://debuggingisfun.blogspot.com/2014/11/android-back-stack-fragment-state-loss.html My problem was I was restoring state in the onCreateView() , which was never called since it was on backstack. In the article they suggest: " Meanwhile, application developers can use onCreate() callback to restore state in the fragments. Fragment Manager at least invokes onCreate() for all fragments in the back stack. ... This ensures each new instance of the Fragment having valid data set at all times." – vankiz Jul 30 '15 at 14:44
  • 1
    That's freaky. I found and read that exact same article yesterday and we realised after worrying that we couldn't restore state in either onCreateView or onActivityCreated that we could use plain old onCreate. I'd just come back on here to answer my own question!!! But I'll give you the green tick. – Russ Wheeler Jul 31 '15 at 09:24
  • @RussWheeler That was spot on! I was trying to get my head around it for hours. Thanks bunch! – Renjith May 11 '17 at 13:36