4

It seems that there is a large amount of information about saving Activity state, but I have been unable to locate much on finding Application state.

I am looking for some design ideas to solve a problem I have run into. I am developing a game that has a fairly large data model (1-2 MBytes). This model exists outside of any Activity, in fact there are numerous activities that all interact with it. These activities are transient, coming and going all the time.

I currently keep a pointer to the data model in my application and all of the activities access the data model through it. I need to save that data model in the event that my application is being killed, but it is far too slow to save it every time an activity hits onPause, which happens very frequently as activities come and go.

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

I would appreciate any suggestions.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
user1609010
  • 43
  • 1
  • 4
  • see [this](http://stackoverflow.com/a/11688697/1289716) – MAC Aug 25 '12 at 12:06
  • @gtumca-MAC I doubt his data model is a pair key-value, even so 1-2 MB to `SharedPreferences`? To the OP maybe you could reduce the data size so you can reduce the overall time? – user Aug 25 '12 at 12:13
  • Luksprog - there are some things I could do to possibly get the portion that I write to a "save" file smaller, maybe even as small as 300-400 KBytes. But that is still too big to write every time one of my many activities call OnPause. I really wish I could find some callback that allows me to know when I am exitng. – user1609010 Aug 25 '12 at 12:17
  • 1
    if your data size is that much large you need to use database. bcoz as i know there is only two ways to store data 1 `SharedPreferance` and second is `Database` – MAC Aug 25 '12 at 12:18

1 Answers1

5

I have been unable to locate much on finding Application state.

That's because there is no "Application state" in Android, any more than there is in a Web app.

but it is far too slow to save it every time an activity hits onPause

While your entire data model may be "1-2 MBytes", but the amount of data that changes is going to be a small subset of that, for any given change. Use a background thread and only modify the data that has changed.

which happens very frequently as activities come and go

It sounds like perhaps you have too many activities.

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

That is not possible. You will never find out that you are being destroyed. Android can and will terminate your process without warning, either at user request (e.g., Force Close, task killer) or for OS reasons (e.g., need the RAM to handle an incoming phone call).

You are welcome to use onUserLeaveHint(), which is called in a number of cases when you entire app loses the foreground, but I certainly would not count on that for something as important as persisting a data model.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Is there any benefit to putting the data model in a Local Service? The documentation there says that onDestroy will be called before the service is destroyed, but I don't know if that is true or not. – user1609010 Aug 25 '12 at 12:52
  • @user1609010: That only helps if you would know when to call `stopService()`. In your case, that does not sound probable. – CommonsWare Aug 25 '12 at 13:27
  • My thinking was that the service would not be destroyed without it's onDestroy() being called. If the service holds the data model then I can save the data model when onDestroy() is called. If the service is still alive when the app restarts then I can grab onto the existing service and hence the data model. If not, I can start the service which will load the data model. – user1609010 Aug 25 '12 at 13:39
  • @user1609010: "My thinking was that the service would not be destroyed without it's onDestroy() being called" -- `onDestroy()` is not guaranteed to be called on any component. Do **not** wait until `onDestroy()` of **anything** (`Activity`, `Service`) to persist a data model. – CommonsWare Aug 25 '12 at 13:43
  • OK. Thanks for the help. I think I will need to use a SQLLite database to store the current state of the data model all the time (in essence moving the datamodel into SQLite). That way I can easily make incremental changes. I can't think of any other easy way to do that. – user1609010 Aug 25 '12 at 14:28
  • @user1609010: SQLite is certainly a reasonable approach for many situations. – CommonsWare Aug 25 '12 at 14:31