简体   繁体   中英

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; nor should I use the savedInstanceState Bundle, because that's unsafe.

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. 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. 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?

  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. 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

In the end, you are welcome to use SharedPreferences for whatever you want. They are optimized for user preferences.

nor should I use the savedInstanceState Bundle, because that's unsafe

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.

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

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. 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.

so I guess I restore the state in its OnResume method.

Each activity is responsible for its own state. Some of that state may well be shared (eg, singleton POJO cache manager). 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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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