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?
On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.
finishActivity(int requestCode) is used to finish another activity that you had previously started with startActivityForResult(Intent, int)
finish() is called to kill your activity instance after you did what you have to do with the activity. After that, you have no use with the activity. So simply call finish() and it will kill your activity.
As per official documentation: You can call finish() from within this function, in which case onDestroy() will be immediately called after onCreate(Bundle) without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.
When calling finish()
on an activity, the method onDestroy()
is executed. This method can do things like:
Also, onDestroy()
isn't a destructor. It doesn't actually destroy the object. It's just a method that's called based on a certain state. So your instance is still alive and very well* after the superclass's onDestroy()
runs and returns.Android keeps processes around in case the user wants to restart the app, this makes the startup phase faster. The process will not be doing anything and if memory needs to be reclaimed, the process will be killed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With