简体   繁体   English

如何保存我的Android应用程序的状态?

[英]How to save the state of my android app?

I want to save the state of my app and restore it, when the user comes back to my app. 当用户返回我的应用程序时,我想保存我的应用程序状态并还原它。 So far I've been reading that I shouldn't use SharedPreferences for that, because that's for preferences; 到目前为止,我一直在阅读,我不应该为此使用SharedPreferences,因为那是出于偏好。 nor should I use the savedInstanceState Bundle, because that's unsafe. 也不应使用savedInstanceState Bundle,因为这是不安全的。

The suggested way is to save data in OnPause to a database or a file (which will suffice in my case) and to restore it in OnResume. 建议的方法是将OnPause中的数据保存到数据库或文件(在我的情况下就足够了),然后在OnResume中还原它。 However, two questions arose regarding this approach: 但是,有关此方法出现了两个问题:

  1. The app can only be entered via the MainActivity, so I guess I restore the state in its OnResume method. 该应用程序只能通过MainActivity进入,所以我想我可以通过其OnResume方法还原状态。 However, the user may leave my app anywhere. 但是,用户可能将我的应用留在任何地方。 Does this mean that I should store the state of my app in the OnPause methods of all of the activities in my app? 这是否意味着我应该将应用程序的状态存储在应用程序中所有活动的OnPause方法中?

  2. If I've understood the activities lifecycle correctly, OnPause and OnResume are also called when the user navigates through the activities in my app. 如果我正确理解了活动的生命周期,则当用户浏览我的应用程序中的活动时,也会调用OnPause和OnResume。 Storing and restoring is not necessary while navigating; 导航时无需存储和还原; I want to store the state only when the user leaves my app and restore it ones he returns. 我只想在用户离开我的应用程序时存储状态并恢复他返回的状态。 Or should I just ignore that and have a lot of storing and restoring going on? 还是我应该忽略这一点,并进行大量存储和恢复?

So far I've been reading that I shouldn't use SharedPreferences for that, because that's for preferences 到目前为止,我一直在阅读,我不应该为此使用SharedPreferences,因为那是出于偏好

In the end, you are welcome to use SharedPreferences for whatever you want. 最后,欢迎您根据需要使用SharedPreferences They are optimized for user preferences. 它们针对用户首选项进行了优化

nor should I use the savedInstanceState Bundle, because that's unsafe 也不应使用savedInstanceState Bundle,因为这是不安全的

It is only for select scenarios . 仅适用于部分情况 It is for keeping a partially-filled-out form values for the user to complete if the user gets distracted for a bit, for example. 例如,如果用户分心了一点,它是为了让用户填写部分填写的表单值。 It is not a long-term storage solution. 它不是长期存储解决方案。

The suggested way is to save data in OnPause to a database or a file (which will suffice in my case) and to restore it in OnResume. 建议的方法是将OnPause中的数据保存到数据库或文件(在我的情况下就足够了),然后在OnResume中还原它。

There are many scenarios and solutions for saving data, depending upon the nature of the data and the user interaction. 根据数据的性质和用户交互,有许多用于保存数据的方案和解决方案。 What you do in a chat app with chat messages that the user types and and you get from the chat server will be very different than what you do in a word processor, which will be very different than what you do in a game, which will be very different than what you do in a streaming media player, etc. 您在聊天应用程序中使用用户键入的聊天消息从聊天服务器中获取的内容与您从聊天服务器中获取的内容将与在文字处理程序中所做的非常不同,在文字处理程序中与在游戏中所做的将会非常不同。与您在流媒体播放器等中所做的非常不同。

The app can only be entered via the MainActivity 该应用只能通过MainActivity输入

That is incorrect, unless you take very specific steps to enforce this. 这是不正确的,除非您采取非常具体的步骤来执行此操作。 If the user is in your app, then moves to another app (eg, presses HOME), your app's process may be terminated while your app is in the background. 如果用户在您的应用程序中,然后移动到另一个应用程序(例如,按HOME),则当您的应用程序在后台时,您的应用程序进程可能会终止。 If the user goes back to your app fairly quickly, though (eg, within a half-hour), such as via the overview screen/recent-tasks list, Android will attempt to return the user to the state they were in last... which could be any of your activities. 但是,如果用户(例如,在半小时内)(例如,通过概览屏幕/最近任务列表)相当快地返回到您的应用,则Android会尝试将用户恢复到上次使用的状态。 ,这可能是您的任何活动。

so I guess I restore the state in its OnResume method. 所以我想我用它的OnResume方法恢复了状态。

Each activity is responsible for its own state. 每个活动都对自己的状态负责。 Some of that state may well be shared (eg, singleton POJO cache manager). 该状态中的某些状态可以很好地共享(例如,单例POJO缓存管理器)。 But an activity should not assume that state has been set up for it previously. 但是,活动不应假定先前已为其建立状态。

Does this mean that I should store the state of my app in the OnPause methods of all of the activities in my app? 这是否意味着我应该将应用程序的状态存储在应用程序中所有活动的OnPause方法中?

Again, that depends a lot on what you are doing in your app. 同样,这很大程度上取决于您在应用程序中的工作。 The answer may be "store the state when the user presses the button" or "store the state when the user swipes" or "store the state as we get it from the incoming MQTT message from the server" or whatever. 答案可能是“在用户按下按钮时存储状态”或“在用户轻扫时存储状态”或“在我们从服务器传入的MQTT消息中获取状态时存储状态”或其他。

I want to store the state only when the user leaves my app and restore it ones he returns 我只想在用户离开我的应用程序时存储状态并恢复他返回的状态

IMHO, you want to store the state when the state changes, in general. 恕我直言,通常情况下,您想存储状态。 "Restore", given appropriate caching, is very fast while the app is in active use and will only be a slow operation if either you have lots of big data (and your cache starts ejecting values) or your process was terminated and started again. 有了适当的缓存,在应用程序处于活动状态时,“恢复”非常快,并且只有在您拥有大量大数据(并且缓存开始弹出值)或者您的进程终止并再次启动时,这才是缓慢的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM