3

I'm confused why anyone would ever override Activity.onDestroy() instead of onPause() if according to the documentation:

There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it,

I see much code that overrides onDestroy() despite this warning. Why?

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246

2 Answers2

3

Why override Activity.onDestroy() if it isn't reliably called?

It's not that it isn't reliably called... it's just that it isn't the only way the Activity can be killed. The Android system might trash your entire process without giving the ActivityManager the chance to call onDestroy() if your device begins to lack memory resources.

For this reason, you shouldn't ever rely on onDestroy() being called, and you should always save persistent state in onPause.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • Right, but what scenarios would require someone to override `onDestroy()` as opposed to always using `onPause()`? – Jeff Axelrod Aug 08 '12 at 15:11
  • 2
    @JeffAxelrod The reason people suggest saving state in `onPause` is because after a call to `onPause`, the `Activity` either has no focus or isn't shown on the screen, making it possible that the Android system could kill the `Activity` to save up memory resources. As a result, unsaved changes are no longer "safe" after `onPause` has been called... hence the common suggestion to make those changes in `onPause` before the `Activity` is pushed out of focus. – Alex Lockwood Aug 08 '12 at 15:25
  • 1
    @JeffAxelrod `onDestroy` is more for cleanup purposes... for example if you've created a `Thread` at some point, you'd want to make sure you kill them in `onDestroy` so that the `Thread` isn't left running once the `Activity` has been destroyed. (Note: You wouldn't have to worry about cleaning up after the `Thread` in the case that the system kills the entire process since the `Thread` lives in the process.) – Alex Lockwood Aug 08 '12 at 15:26
  • 2
    @JeffAxelrod The short and sweet answer is to (1) save changes to information you don't want to lose once the `Activity` is destroyed in `onPause`, and (2) kill valuable resources that you have opened in `onDestroy` to prevent memory leaks. It's not too often that you really need to override `onDestroy` (at least in my experience). – Alex Lockwood Aug 08 '12 at 15:36
0

Objects held by the activity will get destroyed if the process is killed directly. If the process is not killed (and onDestroy() is called) then you will have to manually release the objects if needed. E.g, when the process is killed, a Cursor will be destroyed, but if the process is not destroyed and you repeatedly enter the activity there will be resource leakage.

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • I'm sorry, but I'm not following you. If `onDestroy()` is called, then by definition, the activity will be destroyed. – Jeff Axelrod Aug 08 '12 at 05:47
  • 1
    activity will be destroyed but not the process. So if you have opened the cursor in activity but not closed it, it will leak. So you will have to close it, which you can do in ondestroy. But this case wont arise if process is killed – nandeesh Aug 08 '12 at 05:50
  • @JeffAxelrod the `Activity` [**lives in the process**](http://lh3.ggpht.com/pirklk/SD8-06_E1wI/AAAAAAAACN4/qe7zqqfbXgc/image18.png), so when the process is killed by the kernel, everything contained in the process is wiped out. – Alex Lockwood Aug 08 '12 at 15:13