Simple question: can you be sure that finish()
will call onDestroy()
? I haven't found any confirmation on this.
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()
andonDestroy()
, for reasons independent of whatever is triggering the call tofinish()
A device manufacturer or ROM modder could introduce some screwy change that would break the connection between
finish()
andonDestroy()
The battery could go dead in between
finish()
andonDestroy()
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.
onDestroy()
to be called on that activity. –
Trudytrue 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 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 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!
© 2022 - 2024 — McMap. All rights reserved.