18

Since the deprecation of onRetainNonConfigurationInstance I have been leveraging the framework more and more for Configuration changes. Since I use the ViewPager to hold my main Fragments I cannot use setRetainInstance, which limits my Configuration changes to use onSaveInstanceState like a standard Activity or View would.

It is working perfectly without any problems but I am at the moment passing a quite sizable Serializable dataset through it that makes me want to get the communities input on whether or not it is a good idea.

tl;dr : Does onSaveInstanceState have a size limitation on what you pass through it?

3
  • I think it does not. Probably it uses SharedPreferences -- I guess that.
    – user942821
    Mar 29, 2012 at 1:05
  • "Since I use the ViewPager to hold my main Fragments I cannot use setRetainInstance" -- Why do you think you cannot use setRetainInstance() with fragments in a ViewPager? I cannot find any place that cites this limitation. Got a link? Thanks! Mar 31, 2012 at 10:19
  • It simply does not work, it is probably a bug. I have tried with both the FragmentPagerAdapter and FragmentStatePagerAdapter and neither resulted in the Fragments working correctly with setRetainInstance(). When I moved them from the ViewPager to a standard switch similar to (see below) everything worked correctly. developer.android.com/resources/samples/Support13Demos/src/com/… Mar 31, 2012 at 13:30

2 Answers 2

22
+50

It would be helpful to say something more than "quite sizeable." :)

This data goes through an IPC, and the IPC limit is about 1MB. You want to keep your marshalled size significantly smaller than that; 100K is probably a good maximum. And really you want to keep this as small as possible (think about what you put in there and don't waste space), because this data will have to be held by the system in RAM even when your own process is killed.

0
-1

The limit is the memory limit and since you are not having OutOfMemory errors, that should be fine.

Just make sure to not have any View or Context on the passing object because this can cause the use of memory to grow rapidly if the user changes orientation constantly.

1
  • How is it held in memory if the application is recycled? This information is persisten even then so I know it cannot be held any memory that would depend on the application so of course OutOfMemoryErrors would not occur on the application itself. And I explicitly stated I was passing Serializable objects, it is impossible to pass a View or a Context through that. But if you did pass one through onRetainNonConfigurationInstance it would leak the entire Activity so yes it would eventually run out of memory on Configuration changes in that scenario. Mar 29, 2012 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.