views:

176

answers:

1

As a follow on from my question on sharing state between Activities, how can I save the instance state of my Application? Since Application does not extend Activity, there is no onSaveInstanceState method to override.

NB: In advance, this is not a duplicate. Despite its name, How do I save an Android application’s state? relates to Activity state

+3  A: 

You can't use Application.onTerminate() since there's no guarantee that this will be called.

For the same reason reason you can't use onStop() or onDestroy() from Activity either.

So you'll have to do your save in the onPause() method in every Activity. Each Activity will have a call a saveState() you've created in your Application. Since this will get called a lot you will need to make it as efficient as possible, ideally only writing changed data to persistent storage.

You should also note that onSaveInstanceState() should be used only for storing the transient state of an Activity. For example, if an Activity is ended by the user pressing the Back button onSaveInstanceState() is not called. So even if you didn't have shared state, you should still be using onPause() to save persistent changes.

Dave Webb