坚持对configurationChanges与setRetainInstance在ListAdapter列表中的项目 [英] Persisting list items in ListAdapter on configurationChanges with setRetainInstance

查看:110
本文介绍了坚持对configurationChanges与setRetainInstance在ListAdapter列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ArrayListAdapter 。在配置更改列表不会重新创建。我想坚持的列表。我已经使用 onSaveInstance()的方法来做到这一点,但我知道这是不是在哪里存储这类数据的最好的地方。

一个更合适的方法是 onRetainNonConfigurationInstance()但一直pcated赞成 setRetainInstance的()去$ P $ 中的片段。我不知道如何在片段中setRetainInstance可以取代onRetainNonConfigurationInstance出现上述情况。

感谢

解决方案
  

我不知道如何在片段中setRetainInstance可以取代onRetainNonConfigurationInstance出现上述情况。

如果您的片段通过 FragmentTransaction 动态添加,和你调用 setRetainInstance(真)上的片段,当设备发生更改配置,Android将保留现有的片段实例,并重新使用在新创建的活动。在其他情况下,Android将放弃原来的片段,创造一个全新的片段,比如去与全新的活动实例。如果您的片段实例被保留,其所有数据成员被保留下来,所以你的的ListView 将被保留,同时其配置的 ListAdapter 和一切。

因此​​,关键的问题是:在你的数据模型的ArrayList中的部分数据,或不

如果它是你的数据模型的一部分​​,你应该在某处坚持它(数据库,JSON文件等),然后你可以简单地重新加载的全新片段实例。

如果这不是你的数据模型的一部分​​,那么 setRetainInstance(真),或可能是的onSaveInstanceState() ,将是合适的选择。

I have an ArrayListAdapter. On configuration changes the list is not recreated. I would like to persist the list. I have used onSaveInstance() method to do this but I know it is not the best place where to store this kind of data.

A more appropriate method would be onRetainNonConfigurationInstance() but it has been deprecated in favor of setRetainInstance() in a Fragment. I don't understand how the setRetainInstance in Fragment could replace onRetainNonConfigurationInstance for the above situation.

Thanks

解决方案

I don't understand how the setRetainInstance in Fragment could replace onRetainNonConfigurationInstance for the above situation.

If your fragment is dynamically added via a FragmentTransaction, and you call setRetainInstance(true) on that fragment, when the device undergoes a configuration change, Android will retain the existing fragment instance and reuse it in the newly-created activity. In all other cases, Android will discard the original fragment and create a brand-new fragment instance to go with the brand-new activity instance. If your fragment instance is retained, all of its data members are retained, so your ListView will be retained, along with its configured ListAdapter and everything else.

So, the key question is: is the data in your ArrayList part of your data model, or not?

If it is part of your data model, you probably should be persisting it somewhere (database, JSON file, etc.), and then you can simply reload it in the brand-new fragment instance.

If it is not part of your data model, then setRetainInstance(true), or possibly onSaveInstanceState(), would be appropriate options.

这篇关于坚持对configurationChanges与setRetainInstance在ListAdapter列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆