5

How does one best retain data within a Fragment as its activity goes through an onCreate/Destroy cycle like from Rotation?

In our setup we have potentially large lists loaded from our servers into the fragments custom list adapter and we want to smooth out the UX by not making them reload on rotation. The problem we had with setting the fragment retainInstance=true; is that our adapter has a reference to the context of the original activity and would therefore leak memory. Could we just store the data in the fragment and re-create the adapter; amd if so is that really proper practice?

The next idea is to store the data into a session singleton object and retrieve after rotation which presents a few problems with stale data but we can easily overcome.

The other alternative I see, that seems like it is the *best solution, is to save the data into a bundle and restore into the new fragment after rotation; However, we have quite a few objects that would need to be stored throughout the app and some of our objects are complex, contain lists, multiple types, and would be a pain to make parcelable. Is there a better solution or do we have to bite the bullet and make them Parcelable?

Zachary Moshansky
  • 1,673
  • 18
  • 32
  • How about you pass the application context to the adapter rather than the activity? If you're not doing tricky stuff with differently themed contexts, I'd recommend using the application context anyhow, as that will not change for the lifetime of your app. – MH. Aug 08 '12 at 03:27

3 Answers3

4

Just prevent the Activity from recreating itself on rotation (etc). Add

android:configChanges="keyboardHidden|orientation|screenSize"

to your Activity definition in your AndroidManifest.xml. Then there's no need for saving anything on rotation.

EDIT:

If you don't like that solution then you've got no choice but to use the onSaveInstanceState mechanism. If you've got complex data, just make your classes Serializable and add them to the Bundle that way.

Christopher Perry
  • 38,891
  • 43
  • 145
  • 187
  • 3
    As this is heavily discouraged in the Android documentation and elsewhere we aren't looking at this as a viable option. http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance() – Zachary Moshansky Aug 07 '12 at 20:49
  • 2
    Then you've got no choice but to use the onSaveInstanceState mechanism. If you've got complex data, just make your classes Serializable and add them to the Bundle that way. – Christopher Perry Aug 07 '12 at 20:58
  • 1
    If you make that into an answer it will be accepted as correct. The best solution is to make the data parcelable and use the save instance state but due to complexity this can be mitigated by using the retain instance feature of fragments and more carefully managing our fragments resources to ensure we don't keep references to the old activity/views(which should be done anyways as best practice and to improve performance). – Zachary Moshansky Aug 10 '12 at 18:39
1

Setting the

android:configChanges

attribute in Android manifest is the hackiest and most widely abused workaround for disable the default destroy-and-recreate behavior.

See more about that at

Handling Configuration Changes with Fragments :

http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

Puzzle
  • 173
  • 2
  • 6
0

According to http://developer.android.com/guide/topics/resources/runtime-changes.html,you completely can save the data in the fragment as long as it isn't associated with the activity, views, etc. Bundles are really not meant for heavy amounts of data and serializing is slow, so a fragment is ideal for large amounts of data.

It might not be possible for you to completely restore your activity state with the Bundle that the system saves for you with the onSaveInstanceState() callback—it is not designed to carry large objects (such as bitmaps) and the data within it must be serialized then deserialized, which can consume a lot of memory and make the configuration change slow. In such a situation, you can alleviate the burden of reinitializing your activity by retaining a Fragment when your activity is restarted due to a configuration change. This fragment can contain references to stateful objects that you want to retain.

Daniel Handojo
  • 612
  • 5
  • 19