1

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?

the_prole
  • 8,275
  • 16
  • 78
  • 163

2 Answers2

2

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

Trash Can
  • 6,608
  • 5
  • 24
  • 38
0

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:

enter image description here

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

OBX
  • 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