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.