Is is true that when your application needs to save some persistent data you should always do it in onPause()
method in addition to the onStop()
method, because if android OS kills your process then onStop()
and onDestroy()
methods are never called?
2 Answers
You should save any critical data in onPause
because it is always called when your activity loses focus or user is exiting the app. On the other hand, onStop
is only called when the user is exiting the app, but not when they switch to a different activity(loses focus). Also, when your activity is in the background, android will forcefully kill your app process on low memory in which case onStop
will not be called, onDestroy
is called. So, onPause
is where you should run your persistence code, no need to do it in onStop

- 6,608
- 5
- 24
- 38
-
on pause is the only guaranted place. – Konstantin Pribluda Jan 21 '17 at 10:23
-
@KonstantinPribluda My answer is the verbose version of your comment – Trash Can Jan 21 '17 at 10:23
-
Looking at the life cycle diagram, there seems to be a use case where the app is killed without `onPause()` being called. – the_prole Jan 21 '17 at 19:24
-
@the_prole what? `onPause` is **always** called. The diagram clearly shows that too – Trash Can Jan 21 '17 at 19:35
Is is true that when your application needs to save some persistent data you should always do it in onPause() method in addition to the onStop() method
SHORT ANSWER : TRUE , onPause()
is even more reliable than onStop()
. And the chances of killing the process without invoking onStop()
and onDestroy()
are the highest.
WHY ?
The Activity life cycle diagram is the best aid in this scenario:
You can clearly see the order in which the methods are invoked. The safest choice as you can see is the onPause()
, which provides the first indication that the user is leaving your Activity, and then comes the onStop()
which indicates the app has entered to the Stopped state . And finally comes the onDestroy()
method. So it's safest to choose onPause()
to save the persistent data at the earliest.
FURTHERMORE : The chance and correlation between Activity states and ejection is clearly provided in this table : Activity state and ejection from memory

- 6,044
- 7
- 33
- 77
-
See, that's what I thought. Saving data in `onPause()` and `onStop()` catches all possible use cases. Sure, maybe `onPause()` is the "safest" but I want the app to be foolproof, not just safe. – the_prole Jan 21 '17 at 19:26
-
In what situation would the app skip onPause() and go straight to onStop()? – committedandroider Jul 11 '18 at 05:44