2

This is what I read from a book:

The activity can be destroyed silently after onPause(). We should never assume that either onStop() or onDestroy() is called.

But according to the documentation, Pause refers to partly visible, can an activity partly visible be killed without calling onStop or onDestory?

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
molikto
  • 536
  • 3
  • 15

2 Answers2

7

There is no guarantee that onStop or onDestroy will be called. In situations when memory is severely lacking, the partially visible and out-of-focus Activity may be destroyed to reclaim resources. However, there is no guarantee that either of the two mentioned lifecycle methods will be called before doing so. This is why it is important to save persistent state in onPause instead of onStop and onDestroy.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • Won't it looks awful when a partially visible activity get killed? The visible part just disappear? – molikto Jul 09 '12 at 13:33
  • Yeah... but I find that the system won't destroy partially visible background activitys too often (esp now that phones are so powerful). But it is still important to account for it. – Alex Lockwood Jul 09 '12 at 13:39
  • @AlexLockwood +1 for great clarification. When you say *"This is why it is important to save persistent state in onPause"*, are you implying that `onPause` is **guaranteed** to be called before being killed by the system? – ateiob Aug 08 '12 at 21:16
  • 1
    @ateiob Well, let's put it this way... if the system ever killed off an `Activity` before `onPause`, then chances are the user's phone is completely eff-ed and is about to crash/reboot due to lack of available memory resources. The Android system puts the current foreground `Activity` (the one the user is interacting with) as its top priority to prevent ruining the user experience, so something like this would be both rare and sudden (and thus there wouldn't be a great way to respond to such an event). You shouldn't have to worry about accounting for it. – Alex Lockwood Aug 08 '12 at 23:07
  • Since many apps today target >= Android 4.0 ICS, isn't it safe to assume that `onStop` will *always* be called? Even the training guide says something like, "You should avoid performing CPU-intensive work during onPause(), such as writing to a database, because it can slow the visible transition to the next activity (you should instead perform heavy-load shutdown operations during onStop())" Perhaps you should update the answer to reflect this change :) – Vicky Chijwani Feb 26 '15 at 12:30
  • 1
    @AlexLockwood this answer should be updated, since (as the documentation says) **Starting with Honeycomb, an application is not in the killable state until its onStop() has returned.** – GVillani82 May 25 '15 at 17:57
0

Refering to activity lifecycle - if activity is killed by system, it can be killed silently in Pause state within calling onStop or onDestroy.

shmoula
  • 1,053
  • 2
  • 10
  • 33