What Is Activity.Finish() Method Doing Exactly

What is Activity.finish() method doing exactly?

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

How does Activity.finish() work in Android?

Does it exits immediately or completes
the function from which it was called
?

The method that called finish() will run to completion. The finish() operation will not even begin until you return control to Android.

finish() and the Activity lifecycle

You are correct. onPause, onStop, onDestroy.

Here are the docs.

Proper role of finish() in Android activity lifecycle

There is no single correct answer to your question. It depends a lot on what the workflow in your app is.

When ActivityA launches ActivityB and does not call finish(), then ActivityA is still present in the Activity stack in the task. When ActivityB ends, ActivityA will be shown as it is now the top Activity in the stack.

When ActivityA launches ActivityB and calls finish() on itself, then ActivityA is no longer present in the Activity stack in the task. When ActivityB ends, ActivityA will not be shown as it is no longer in the stack. The Activity underneath ActivityA will be shown (if there is one), otherwise the task ends as there are no longer any live activities in it.

Android: When to end class with finish()?

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

What does this.finish() really do? Does it stop my code running?

Chris, I am no expert, but at the answer here about finish() in android is basically what codeMagic just said. The link is valuable because of the discussion regarding onStop() and onDestroy()

finish() finish the activity but it is still on background

try using

android:excludeFromRecents="true" in your Activity in AndroidManifest.xml.

<activity
...
android:excludeFromRecents="true">
</activity>

What can keep activity alive after finish()?

I think this answer can help you
https://stackoverflow.com/a/10862977/13221053
Further, if you want to keep your app active in background then you can use services for that purpose, here is the documentation
https://developer.android.com/guide/components/services

Android: Calling Activity.finish() method

This is basically a duplicate of quitting-an-application-is-that-frowned-upon? In short, when you finish an activity, the activity gets destroyed but not the process. This is an important aspect of Android programming and is important to understand how it's different from other platforms. Read the post I linked to for lots of details.



Related Topics



Leave a reply



Submit