保存 Android 列表视图的高级状态
我知道 Android 提供了一种机制,允许开发人员通过以下方法保存和恢复 Activity 中的状态:
protected void onSaveInstanceState (Bundle outState)
protected void onRestoreInstanceState (Bundle savedInstanceState)
我见过很多示例,人们展示了如何保存简单状态(例如列表视图中的当前索引),还有很多人们说列表视图应该由允许恢复列表的数据模型支持的示例。然而,当事情变得更加复杂并且用户交互更加复杂时,我一直在努力寻找保存此状态的正确方法。
我有一个列表视图,其中包含每行的自定义控件。单击时,一行将以动画展开,向用户显示附加详细信息...在附加详细信息视图中,用户还可以更改某些内容。现在我知道,如果我的 Activity 被销毁(或者我的 Fragment 被替换),反映 UI 更改的当前状态以及用户与列表的交互现在就会丢失。
我的问题是如何正确保存列表视图的详细状态,以便也保留每个自定义组件行的状态?
谢谢!
编辑:感谢以下回复。我知道 SharedPreferences 可用于存储键值状态,但是,我想我对视图状态更感兴趣......例如 Android 会自动保存/恢复 EditText 字段的视图状态(即其内容)我的活动,那么我如何在列表视图中使用更复杂的自定义组件来实现相同的目标呢?我真的必须自己手动保存此状态吗(即准确保存已展开的列表视图行,保存用户所做/切换的所有 ui 更改等)?
I know that Android provides a mechanism to allow developers to save and restore states in an Activity via the following methods:
protected void onSaveInstanceState (Bundle outState)
protected void onRestoreInstanceState (Bundle savedInstanceState)
I've seen numerous examples whereby people show how to save simple state (such as the current index in a listview), and many examples where people say that the listview should just be backed by a data model which will allow the list to be restored. However, when things get more complicated and the user interaction more involved, I've been struggling to find out the correct way to save this state.
I have a listview which contains custom controls for each row. When clicked a row will expand with animation to show additional details to the user... within the additional details view, the user is also able to change certain things. Now I know that if my Activity is destroyed (or my Fragment replaced), the current state reflecting the UI changes and the user's interaction with the list is now lost.
My question is how do I got about correctly saving this detailed state of my listview such that the state of each custom component row is also preserved?
Thanks!
Edit: Thanks for the replies below. I understand that SharedPreferences can be used to store key-value state, however, I guess I am more interested in the view states.... for instance Android will automatically save/restore the viewstate of an EditText field (i.e. its contents) in my activity , so how do I go about achieving the same thing with more complex custom components in a listview? Do I really have to save this state all manually myself (i.e. save exactly which listview rows have been expanded, save all the ui changes the user has made/toggled, etc.)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不喜欢这么说,但是看来这一切都得自己来完成。该框架不提供“保存视图状态”的预制方法。
无论您将状态保存在 Bundle 还是 SharedPreferences 中,我认为要点是您需要确定反映当前列表选择的状态变量,自己保存它们,以及
onResume() 您需要将该状态重新引入您的活动中。
...正如你所说,这是一个手动过程...
I don't like to say this, but it seems that this must all be done yourself. The framework does not offer a pre-made method for "save state of view".
Whether you save the state in a Bundle or SharedPreferences, I think the point is that you need to determine state variables that reflect your current list choices, persist them yourself, and
onResume()
you need to reintroduce that state into your activity.... quite a manual process, as you say ...
我不确定,但我认为 SharedPreferences 是您可能正在寻找的。
I am not sure but I assume SharedPreferences is what you might be looking for.
在 onPause() 方法中使用 SharedPreferences 。
当它移至后台时,您可以在其中存储所有活动数据。
当您想要恢复从共享首选项获取的数据时。
Make use of SharedPreferences in onPause() method.
where you can store all you activities data when it is moving to background.
When ever you want to restore the data fetch from the Shared Preferences.