What is Activity.finish() method doing exactly?

Tal Kanel picture Tal Kanel · Jun 1, 2012 · Viewed 222.4k times · Source

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?

Answer

K_Anas picture K_Anas · Jun 2, 2012

When calling finish() on an activity, the method onDestroy() is executed. This method can do things like:

  1. Dismiss any dialogs the activity was managing.
  2. Close any cursors the activity was managing.
  3. Close any open search dialog

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