Xander Xander - 2 months ago 12
Android Question

Android: When to end class with finish()?

I often see examples of classes which end with

, but definitely not always. My question is when should you end a class with
? And what does it do exactly, what is the difference between ending a class with the back button and ending it with

Thanks in advance!


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