Android Fragments Retaining Data

久未见 提交于 2019-11-28 08:07:48

问题


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?


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/11853389/android-fragments-retaining-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!