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:
- Dismiss any dialogs the activity was managing.
- Close any cursors the activity was managing.
- 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
Related Topics
How to Check If a File Is Open by Another Process (Java/Linux)
Why Does Tomcat Work with Port 8080 But Not 80
Difference Between Using Java.Library.Path and Ld_Library_Path
Setting/Changing the Ctime or "Change Time" Attribute on a File
How to Find My Pid in Java or Jruby on Linux
Error Playing Audio File from Java via Pulseaudio on Ubuntu
Java's 'Tnameserv' Takes 3+ Minutes to Be "Ready", Why
Cassandra Startup Error 1.2.6 on Linux X86_64
Linux Start-Up Script for Java Application
How to Set Classpath in Linux to Let Java Find Jar File
How to Solve the 'Lock Obtain Timed Out' When Using Solr Plainly
What Is Contained in Code/Internal Sections of Jcmd
How to Support Both Ipv4 & Ipv6 on Java
Java System Preferences Under Different Users in Linux
What Would Cause a Java Process to Greatly Exceed the Xmx or Xss Limit