Android: Will finish() ALWAYS call onDestroy()? [duplicate]
Asked Answered
D

2

28

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

Designing answered 10/11, 2013 at 16:15 Comment(3)
#10848026Isopiestic
not always, as reported in the documentationBonhomie
@blackbelt, could you refer to that specific documentation?Designing
T
51

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.

Trudytrue answered 10/11, 2013 at 16:21 Comment(7)
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.Confusion
@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.Trudytrue
Too fast haha. Thank you!Confusion
When exactly would calling finish() not trigger a call of onDestroy (assuming onDestroy wasn't called yet and the app is still alive) ?Microcircuit
@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.Trudytrue
@Trudytrue 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.Microcircuit
@Trudytrue 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).Microcircuit
S
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! 
Slunk answered 10/11, 2013 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.