Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Activity.finish() method doing exactly?

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?

like image 851
Tal Kanel Avatar asked Jun 01 '12 09:06

Tal Kanel


People also ask

What does activity finish do?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

What is finish Activity in Android Studio?

finishActivity(int requestCode) is used to finish another activity that you had previously started with startActivityForResult(Intent, int)

What does Java finish do?

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.

What happens when finish () is called inside onCreate ()?

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.


1 Answers

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

like image 83
K_Anas Avatar answered Sep 30 '22 21:09

K_Anas