简体   繁体   中英

What happens to the code when someone closes or kills an app?

I have been looking into storing data for my app and trying to choose between different methods ( onSaveInstanceState , onPause/onResume ) and different methods of storing(states in onSaveInstanceState, SQLite , Prefences ).

I am curious what happens to each of these methods of storing when the user does certain things. In specific, I want to know what methods are called and what data is wiped when:

  • User clicks the task switcher button(bottom right) and then closes the overlay within seconds, all from within the app
  • User clicks home without swiping out app from task switcher, then reopens app
  • User clicks home, swipes out the app from task switcher, then reopens app
  • User exits app, restarts phone, then opens app
  • User exits app, and uses Clean Master (or any other storage manager) to clear the cache of all apps or kill all background tasks, then reopens app.
  • User updates app
  1. When the user goes to task switcher and closes the app, then the app process will get killed and any services running in the background will also be stopped
  2. When the user clicks home, without swiping out from the task switcher and reopens the app, then the app is getting resumed. You can get further details if you search for activity lifecycle
  3. When the user clicks home and swipes out the app from task switcher, then it is equivalent to killing the app and the process will get killed and any services running in the background will also be stopped
  4. When the user exits the app and restarts.reboots the phone, then the process is killed and services also will get killed. But after restarting/rebooting the device, user would have written logic to restart it once the device is restarted or app is killed
  5. When the user exits the app and clears cache, then I would assume that the data is cleared. So this is like a fresh app.

Based on my understanding, I have given my brief answer. Hope this gave some insights.

I'm trying to store highscores. Currently i'm just using savedpreferences and putting 5-6 numbers in an editor, committing, and continuing. there might be a more efficient method, which is why I elaborated on different possibilities above.

I assume you meant SharedPreferences. And yes, you're doing it the right way. Your high scores will be saved between app executions. And doing the same with SQLite would be way overkill.

The only way that data can be cleared by the user will be if he clicks on Clear the Data or if he clicks to Uninstall your application

However, neither uppdating the app itself, nor clicking on Clear the cache will do anything to the SharedPreferences, the user will have to press on that button "Clear the Data" if he wants that specific data to be cleared.

As to a more efficient method, it depends what you mean by that. What you're doing is what almost every developer would be doing. It's the simplest solution for the job that actually works. For instance, if you had chosen to store that data in a bundle between activities, then all that high score data will be lost between app executions.

That being said, your solution would obviously not be enough if you were to play the same game on multiple devices, or if you would migrate to a new device, or if you were to uninstall the game and reinstall it at a later time. For that, you may want to consider using Google Play Services to store that data for you, both locally and on the cloud.

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