Android - 保存活动状态和列表视图项

发布于 2024-12-11 14:56:58 字数 160 浏览 0 评论 0原文

我目前有一个由用户自行填充的 listview 。我希望这样当用户离开活动并返回时,用户添加的所有内容仍然在 listview 中。我一直在阅读有关 onSaveInstanceState 的内容,但我不知道这是否是正确的方法。任何帮助将不胜感激。谢谢。

I currently have a listview that is self populated by the user. I would like it so that when the user leaves the activity and comes back, everything the user added is still in the listview. I have been reading about onSaveInstanceState, but I don't know if that is the right way to do it. Any help would be greatly appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╭ゆ眷念 2024-12-18 14:56:58

它并不完全是您所需要的; onSaveInstaceState 在系统终止您的 Activity 以回收资源(内存不足)之前保存实例,而不是在用户显式退出它时保存实例。

如果您想要保存活动状态,以便每当用户输入时它都会保持不变,则必须将数据保存在文件/共享首选项/使用内容提供程序上。
如果您只想保存单个列表视图项目的本地数据,我建议使用共享首选项,因为它非常简单直观。 此处了解它们。

Its not exactly what you need; onSaveInstaceState saves the instance before the system kills your activity to reclaim resources (low memory), not if the user explicitly exits it.

If you wamt to save the activity state so whenever the user enters it will stay the same, you must save the data on file/shared preferences/using content providers.
If all you want to do is to save local data of a single listview's items, I'll recommend using Shared Preferences as it's very easy and intuitive. Read about them here.

夏日落 2024-12-18 14:56:58

我假设如果用户正在填充一些数据,那么该数据在某处有存储(SQLite、云等...)。我建议积极存储用户输入的任何内容(即,一旦他们单击“新建”并输入新列表项,就推送到数据库并刷新列表)。因此,当调用应用程序 onResume() 时,您可以从其源中获取数据并重新填充列表视图。暂时保存到 SharedPreferences 中听起来很麻烦(对于项目列表)。

I am assuming if the user is populating some data, then that data has a store somewhere (SQLite, cloud, etc...). I recommend aggressively storing anything the user enters (i.e. as soon as they click "new" and enter the new list item, push to the database and refresh list). So when the applications onResume() is called, you can grab the data from its source and re-populate the list view. Saving temporarily into SharedPreferences sounds cumbersome (for a list of items).

江心雾 2024-12-18 14:56:58

如果活动在离开和返回之间没有被销毁,那么您的数据应该位于用户离开的位置(除非您在恢复时进行了一些重新初始化)。但是您不能指望这一点,因为 Android 可能会在需要时释放资源,并且非运行的 Activity 可能会成为此类行为的受害者。

因此,正如前面提到的,您应该将数据保存到持久存储中。如果只是针对这个 Activity,并且只有少量数据,那么 SharedPreferences 是一个很好且简单的解决方案。在 onPause() 中保存,在 onResume() 中加载,只有当手机死机且 Activity 处于前面时,您才会丢失数据。

我不建议为此使用文件,但如果您的数据必须通过不同的视图访问、处理、排序、过滤等,那么您可能需要考虑 SQLite。

If the Activity is not destroyed between leaving and returning then your data should be where the user left it (unless you do some reinitialization in on resume). However you can't count on that, as Android may free up resources when needed, and non-running Activitys may fall victim of such an act.

Therefore you should - as forementionned - save your data to persistent storage. If it's just for this Activity, and it's only a small amount of data, then SharedPreferences is a good and easy solution. Save in onPause(), load in onResume() and you'll only lose data when the phone dies with your Activity up front.

I wouldn't recommend using files for this, but if your data has to be accessed by different views, processed, sorted, filtered etc. than you may want to think about SQLite.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文