Search code examples
androiddatabasesqlitestateandroid-savedstate

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

Solution

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