Android Fragments Retaining Data

2020-02-14 04:14发布

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?

3条回答
相关推荐>>
2楼-- · 2020-02-14 04:48

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.

查看更多
\"骚年 ilove
3楼-- · 2020-02-14 04:57

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.

查看更多
成全新的幸福
4楼-- · 2020-02-14 04:59

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

查看更多
登录 后发表回答