3

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
Community
  • 1
  • 1
John Targaryen
  • 1,109
  • 1
  • 13
  • 29
  • 1
    Please specify the kind of app you will be making and the kind of data you will be storing. Telling you "exactly what methods are called in these proccesses" will just take too much time otherwise. Your specific use case will drive the choice of your architecture and your architecture will drive the choice of callbacks you will need to make use of. – Stephan Branczyk May 03 '15 at 23:58
  • @StephanBranczyk thats a good idea; 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. – John Targaryen May 04 '15 at 02:19

2 Answers2

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

Santhana
  • 233
  • 1
  • 5
  • 2
    #1, #3, #4 are incorrect: Removing an app from Recent Apps (what Google calls the screen now) will _not_ stop any services. It will only remove the app from the screen and maybe release the memory used by Activities. That's why even after swiping away GMaill you can still get new email notification. "Exiting" an app by itself will not cause the process to be killed. – Kai Apr 22 '15 at 05:50
  • While this answer is definitely useful, I was looking for something more specific: exactly what methods are called in these proccesses. – John Targaryen Apr 24 '15 at 00:40
  • 1
    #5 I believe might also be incorrect. Many people clear caches, like [Reddit - Clean Master Functions Thread](http://www.reddit.com/r/androidapps/comments/1xz8ef/are_apps_like_clean_master_necessary/), and it doesn't seem to clear their data. – John Targaryen Apr 24 '15 at 01:01
  • 1
    @JoeBob, Ignore the above answer. Most of it is incorrect anyway. Also #6 wasn't mentioned, but the data survives if it was stored in SQLite even after an update of the app itself (even if the schema was changed, assuming the developer wants it to). – Stephan Branczyk May 04 '15 at 00:02
1

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.

Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49