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
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()oronDestroy()fromActivityeither.So you’ll have to do your save in the
onPause()method in every Activity. EachActivitywill have a call asaveState()you’ve created in yourApplication. 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 anActivity. For example, if anActivityis ended by the user pressing the Back buttononSaveInstanceState()is not called. So even if you didn’t have shared state, you should still be usingonPause()to save persistent changes.