How Does Activity.Finish() Work in Android

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.

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

finish() and android activity lifecycle

Yes the code after the call to finish() will be called.

when you call finish() the next function that will be executed is onPause().

If you want to close all activities on the back stack do this:

setResult(RESULT_CLOSE_ALL);
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()

How to finish Activity when starting other activity in Android?

You need to intent your current context to another activity first with startActivity. After that you can finish your current activity from where you redirect.

 Intent intent = new Intent(this, FirstActivity.class);// New activity
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - Clears the activity stack. If you don't want to clear the activity stack. PLease don't use that flag then.

finish not stoping the activity code execution

No, finish is not an abort. The function will continue, and when it gets back to the Looper inside the Android framework running the event loop it will begin the deinitialization sequence (calling onPause, onStop, and onDestroy).

Android-finish function in activity didn't work

Start all activity like below:

Intent i = new Intent(this,NewActivity.class);

startActivity(i);

don't add finish() method, then inside button click just call finish() method. It will bring you back to previous activity.

Passing finish activity before all lines are executed - android


If we call the acitivty.finish() and there are some lines of code below that. will that be executed once the finish() has been called?

Yes Because acitivty.finish() is not an abort

CHECK this Example

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("TEST", "BEFORE FINISH");
finish();
Log.e("TEST", "AFTER FINISH");
Log.e("TEST", "AFTER FINISH");

}

RESULT

Sample Image



Related Topics



Leave a reply



Submit