Is there a natural place to save the state of an Android app as it is shutting down? At the moment I am doing it in onDestroy()
for my main activity, then reloading in onCreate()
for the same activity. Is that the right place for it?
Asked
Active
Viewed 87 times
0

ozbek
- 20,955
- 5
- 61
- 84

William Jockusch
- 26,513
- 49
- 182
- 323
1 Answers
0
Apps don't "shut down" in any conventional sense. Your UI may move to the background, and your process will eventually be terminated.
In many cases, the right time to save the state of your app is when the state changes. At worst, save the state of the activity when the activity moves to the background (i.e., in onPause()
or onStop()
).
onDestroy()
is not guaranteed to be called on any component, including activities. Hence, it is not an appropriate place for saving state.
Also note that saving state requires background threads, for any typical definition of the term "saving", if you do not want to drop frames and freeze your UI.

CommonsWare
- 986,068
- 189
- 2,389
- 2,491