I'm developing android applications for a while, and followed a lot of posts about activity life cycle, and application's life cycle.
I know Activity.finish()
method calls somewhere in the way to Activity.onDestroy()
, and also removing the activity from stack, and I guess it somehow points to operating system and garbage collector that he can "do his trick" and free the memory when it find it a good time doing so....
I came to this post - Is quitting an application frowned upon? and read Mark Murphy's answer.
It made me a bit confused about what exactly the finish()
method actually does.
Is there a chance I'll call finish()
and onDestroy()
won't be called?
onDestroy()
is meant for final cleanup - freeing up resources that you can on your own,closing open connections,readers,writers,etc. If you don't override it, the system does what it has to.on the other hand,
finish()
just lets the system know that the programmer wants the currentActivity
to be finished. And hence, it calls uponDestroy()
after that.Something to note:
it isn't necessary that only a call to
finish()
triggers a call toonDestroy()
. No. As we know, the android system is free to kill activities if it feels that there are resources needed by the currentActivity
that are needed to be freed.@user3282164 According to the Activity life-cycle it should go through
onPause()
->onStop()
->onDestroy()
upon callingfinish()
.The diagram does not show any straight path from [Activity Running] to [
onDestroy()
] caused by the system.onStop() doc says "Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called."
finish () just sends back to the previous activity in android, or may be you can say that it is going one step back in application
calling finish in onCreate() will not call onDestroy() directly as @prakash said. The
finish()
operation will not even begin until you return control to Android.Calling finish() in onCreate(): onCreate() -> onStart() -> onResume(). If user exit the app will call -> onPause() -> onStop() -> onDestroy()
Calling finish() in onStart() : onCreate() -> onStart() -> onStop() -> onDestroy()
Calling finish() in onResume(): onCreate() -> onStart() -> onResume() -> onPause() -> onStop() -> onDestroy()
For further reference check look at this oncreate continuous after finish & about finish()
My study shows that
finish()
method actually places some destruction operations in the queue, but the Activity is not destroyed immediately. The destruction is scheduled though.For example, if you place
finish()
inonActivityResult()
callback, whileonResume()
has yet to run, then firstonResume()
will be executed, and only after thatonStop()
andonDestroy()
are called.NOTE:
onDestroy()
may not be called at all, as stated on the documentation.Various answers and notes are claiming that finish() can skip onPause() and onStop() and directly execute onDestroy(). To be fair, the Android documentation on this (http://developer.android.com/reference/android/app/Activity.html) notes "Activity is finishing or being destroyed by the system" which is pretty ambiguous but might suggest that finish() can jump to onDestroy().
The JavaDoc on finish() is similarly disappointing (http://developer.android.com/reference/android/app/Activity.html#finish()) and does not actually note what method(s) are called in response to finish().
So I wrote this mini-app below which logs each state upon entry. It includes a button which calls finish() -- so you can see the logs of which methods get fired. This experiment would suggested that finish() does indeed also call onPause() and onStop(). Here is the output I get: