setRetainInstance not working for ListFragment when using compatibiltiy library
Asked Answered
E

2

5

I'm attempting to save a ListFragment subclass across an orientation change so I added setRetainInstance(true) to the end of my onCreate function. I added an onSaveInstanceState method to add all of it's data to a bundle, then added code into onActivityCreated to load that data back. Unfortunately, it wouldn't work.

When I added some debugging messages with the help of Log.d I discovered that not only was onSaveInstanceState not being called, but onCreate was (which the documentation seems to say shouldn't happen when retainInstance is true). Neither onCreate nor onActivityCreated have bundles with my data (unsuprisingly).

I'm guessing this may be a problem with compatibility library, though I don't have an android 3.0+ device to test this.

Any help is appreciated and I can post some code snippets if necessary, though I'm not doing anything complicated.

Update: onDestroy is not being called when I change orientation (which is how it should be), so it seems that some of setRetainInstance is working

Eldoria answered 2/8, 2011 at 1:56 Comment(4)
I'd suggest debugging the compat lib to see what's going on. What version of the compat lib are you using? I'm using r1 and avoided r2 because there is at least one regression bug with onDestroy not being called when a fragment is replaced.Cosma
I'm using r3, I'll look for an older version to see if that helps. Any suggestions on how to go about debugging the compat lib? I'm very new to debuggin Java.Eldoria
Just try and step into one of the compat lib methods and you should get prompted for the location of sources.Cosma
Took a little while since I'm not working with Eclipse, but I got jdb working, though it wasn't of much help. However, it seems the problem has resolved itself (presumably because of some bit of debugging code I added). When I figure out what fixed it, I'll post an answer.Eldoria
E
10

I finally figured out what my problem was. It all came down to a single line I'd forgot to add. In my FragmentActivity subclass I'd overrode onSaveInstanceState, but I never called super.onSaveInstanceState. Apparently, unlike other methods whose parents I'd forgotten to call, onSaveInstanceState won't throw a runtime error when I forget to call the parent classes version of it, instead setRetainInstance just stops working. I hope this saves someone the headache I went through trying to solve this.

Eldoria answered 4/8, 2011 at 20:16 Comment(2)
I've just faced the same problem. The thing is that you are not obliged to call the method of the superclass, but fragments won't be retained if you don't do so. I had to look through the source code of the compability library to figure this out.Alternation
@Eldoria You don't have to implement onSaveInstanceState for setRetainInstance to work do you?Eastsoutheast
E
1

It seems, when you set setRetainInstance = true while both onSaveInstanceState() and onActivityCreated() are called, then Bundle will not be returned.

However, as the as the ListFragment is being retained, you can simply store its state into a field, and handle it inside onActivityCreated().

Bear in mind, the Activity will still be destroyed and recreated.

Explication answered 13/12, 2011 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.