27

Simple question: can you be sure that finish() will call onDestroy()? I haven't found any confirmation on this.

Xander
  • 5,487
  • 14
  • 49
  • 77

2 Answers2

50

Simple question: can you be sure that finish() will call onDestroy()?

First, this answer assumes that you are referring to Android's Activity class and its finish() method and onDestroy() lifecycle method.

Second, it depends upon your definition of "sure":

  • Your process could be terminated in between finish() and onDestroy(), for reasons independent of whatever is triggering the call to finish()

  • A device manufacturer or ROM modder could introduce some screwy change that would break the connection between finish() and onDestroy()

  • The battery could go dead in between finish() and onDestroy()

  • Etc.

Third, finish() does not call onDestroy(). You can tell that by reading the source code. finish() usually triggers a call to onDestroy().

Generally speaking, finish() will eventually result in onDestroy() being called.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Within a process, can I rely on onDestroy()? Say, I have A, B, C, D, E activities, and want to finish all but E, then will all onDestroy() methods be called? The battery and memory state don't count here. – Jenix Jul 01 '17 at 22:16
  • 4
    @Jenix: If the process is not terminated, and you do not trigger a crash (unhandled exception), finishing an activity will cause `onDestroy()` to be called on that activity. – CommonsWare Jul 01 '17 at 22:18
  • Too fast haha. Thank you! – Jenix Jul 01 '17 at 22:18
  • When exactly would calling finish() not trigger a call of onDestroy (assuming onDestroy wasn't called yet and the app is still alive) ? – android developer Nov 03 '20 at 15:47
  • 1
    @androiddeveloper: `finish()` is not `final`, so it could be overridden, and the overridding implementation might choose to not chain to the superclass in some cases for some strange reason. And, it is at least theoretically possible for a crash to occur between `finish()` and `onDestroy()`, though I don't know of any scenarios for that. – CommonsWare Nov 03 '20 at 15:50
  • @CommonsWare I've seen today a weird case of this. I've overridden `onDestroy` and `finish` and only added logs to see that indeed they get called. It's really weird but for some reason I got `finish` to be called, but not `onDestroy`. That's why I ask. It's the first time I saw such a weird thing. No idea how to fix it either. And it's not like that the UI doesn't get a chance to do anything. I've seen `finish` being called multiple times later and still the Activity didn't call `onDestroy` even once. – android developer Nov 03 '20 at 23:05
  • 1
    @CommonsWare Never mind. I think I got it. I reached a very old code that for some reason had "synchronized" on the current Activity via a background thread, while also calling "setResult" (which synchronizes on the current Activity, too) on the UI thread. So it caused weird issues (probably ANR or deadlock, or something similar). – android developer Nov 04 '20 at 10:48
6

No you cannot be sure!

Calling finish() generally triggers onDestroy() as per the Activity life cycle but you cannot rely on it. Specifically not for saving your data. Documentation clearly says

do not count on this method being called as a place for saving data! 
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289