Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Activity.finish() work in Android?

Tags:

java

android

Could someone provide a description of what happens when an Activity calls its finish() method?

Does it exit immediately, or does it complete the function from which it was called?

like image 477
rantravee Avatar asked Apr 07 '10 08:04

rantravee


People also ask

What is finish Activity in Android Studio?

finishActivity(int requestCode) is used to finish another activity that you had previously started with startActivityForResult(Intent, int)

How do you check if activity is finished or not?

Using activity. isFinishing() is the right one solution. it return true if activity is finished so before creating dialog check for the condition. if true then create and show dialog.

Why do we need to call setContentView () in onCreate () of activity class?

As onCreate() of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.

What is finish method?

finish () just sends back to the previous activity in android, or may be you can say that it is going one step back in application. Follow this answer to receive notifications.


4 Answers

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.

like image 140
CommonsWare Avatar answered Oct 17 '22 20:10

CommonsWare


Every life cycle event like onCreate, onResume, onPause.... onDestroy of an Activity is always called on a single thread - The "Main thread".

In short this thread is backed by a Queue into which all the activity events are getting posted. This thread can execute all these events in the order of insertion.

If you are calling finish() in one of the life cycle callbacks like onCreate()...a "finish" message will get added to this queue but the thread is not free to pick & execute "finish" action until currently executing method returns i.e Thread is freed from current task.

like image 20
user3688593 Avatar answered Oct 17 '22 20:10

user3688593


ondestroy() is the final call you receive before your activity is destroyed.

This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

like image 45
user284295 Avatar answered Oct 17 '22 19:10

user284295


If there are two activities A and B. And your flow is going from A > B; and B=A calls finish().

Then,

The method where you called finish() from will execute as Mark mentioned. And flow of callbacks will be as followed -

  1. onPause() of activity A
  2. onRestart() > onStart() > onResume() of Activity B
  3. Then, comes the real difference. If you did not call finish() from activity A; only onStop() of Activity A will be called here. While, in this case, where we called finish() from Activity A; So onStop() and onDestroy() both will be called for activity A.
like image 40
Darpan Avatar answered Oct 17 '22 21:10

Darpan