Xander
Xander

Reputation: 5587

Android: When to end class with finish()?

I often see examples of classes which end with finish(), but definitely not always. My question is when should you end a class with finish()? And what does it do exactly, what is the difference between ending a class with the back button and ending it with finish()?

Thanks in advance!

Upvotes: 7

Views: 18907

Answers (5)

Rolf ツ
Rolf ツ

Reputation: 8781

finish() can be called to kill (destroy) an Activity instance. If you don't need to close your Activity manual, which is true in many cases, you don't need to call this method.

But if you require a button somewhere in your activity that says "close", then you should use this method. But in general the back button behavior in Android will handle things like this.

The back button does not actually finish your activity, finish() calls the onDestory() method right away, while the back button does not.

When the back button is pressed, the onStop() method is called, but the onDestory() method call might be delayed by the system, this so that the Activity can be resumed by the system which is cheaper (in resources) than a full restart.

Lifecycle: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Finish(): http://developer.android.com/reference/android/app/Activity.html#finish()

Upvotes: 9

Renjith
Renjith

Reputation: 5803

public void finish ()

Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().

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.

If you press back button, it will call finish() automatically to kill the activity. It will go through the activity lifecycle(onPause(), onStop() and finally onDestroy())

The one reason with calling finish() is that, if you don't call it, when you navigate through activities, all the activities will be added to the backstack. So when you go back, you have to come back through these activities. So for e.g when you click back button, it simply kills the activity and it will be removed from the backstack.

Upvotes: 1

ThePCWizard
ThePCWizard

Reputation: 3348

Back button takes you to the last activity in the stack whereas finish() destroys the current activity. You can call finish() on an activity for which you don't want the user return to. Example: LoginActivity

Upvotes: 1

dorjeduck
dorjeduck

Reputation: 7794

One reason to use finish is when you have started the activity with

void android.app.Activity.startActivityForResult(Intent intent, int requestCode)

In this case you dont go back to the Activity which started it with another startActivity but with finish

Upvotes: 2

Mohamed_AbdAllah
Mohamed_AbdAllah

Reputation: 5322

You finish Activity class with finish() method when you need to end the Activity immediatly at specific point. If you press back button, you will go through the lifecycle (onPause(), onStop() then onDestroy()) automatically.

Upvotes: 2

Related Questions