Saving Android app data on application exit

There seems to be a lot of information about maintaining the state of the Activity , but I have not been able to find a lot of information about the state of the Application .

I am looking for some design ideas to solve the problem I am facing. I am developing a game with a fairly large data model (1-2 megabytes). This model exists outside of any Activity; in fact, there are many actions that all interact with it. These actions are transient, come and go all the time.

Currently, I keep a pointer to the data model in my application, and all the actions allow me to access the data model through it. I need to save this data model in case my application is killed, but too slow to save it every time an activity clicks on Pause, which happens very often when actions come and go.

I need a way to determine that my application (and with it my data model) is being destroyed. I searched extensively for this method or callback and came up empty.

I would be grateful for any suggestions.

+4
source share
1 answer

I could not find much information about the state of the application.

This is because there is no “application state” in Android, more than in a web application.

but keeping it too slow every time the action clicks on Pause

So far, your entire data model may be “1-2 megabytes,” but the amount of data that will change will be a small subset of any given change. Use a background thread and only modify changed data.

which happens very often when actions come and go

It seems like you have too many activities.

I need a way to determine that my application (and with it my data model) is being destroyed

It's impossible. You will never know that you are being destroyed. Android can and will terminate your process without warning, either at the request of the user (for example, Force Close, kill), or for OS reasons (for example, it is necessary that the RAM process an incoming phone call).

You can use onUserLeaveHint() , which is called on a number of occasions when the entire application loses foreground, but I certainly won’t count on it for something as important as saving the data model.

+4
source

All Articles