Android: When to end class with finish()?
Asked Answered
S

5

7

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!

Swale answered 16/10, 2012 at 10:1 Comment(0)
C
9

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()

Clericalism answered 16/10, 2012 at 10:3 Comment(0)
M
2

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.

Misbehave answered 16/10, 2012 at 10:5 Comment(0)
N
2

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

Nacelle answered 16/10, 2012 at 10:5 Comment(0)
C
1

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.

Connection answered 16/10, 2012 at 10:9 Comment(0)
P
1

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

Panties answered 16/10, 2012 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.