How does Activity.finish() work in Android?
Asked Answered
B

4

108

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?

Barrage answered 7/4, 2010 at 8:27 Comment(1)
Related thread - what exactly Activity.finish() method is doing?Eccentric
R
127

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.

Radome answered 7/4, 2010 at 11:6 Comment(7)
Then how to finish an activity which is called by StartAvtivityforResults() without completion?Insoluble
@Radome How we can ensure that onStop is called always ? In my app OnStop and onDestroy are not called in some cases and in those cases the child activity takes too much time in finishing and that looks like App has hanged... Whereas when onStop is called everything works normal. I am on Galaxy y with Android 2.3.6... see my question at #13929091 plz help!!!Guardi
@Radome I tested sir very true.+1 for you But I wanna ask one question I have written finish() as an first statement in my function then it is executing the whole function and then finish() is called How this managed.Cesarcesare
@NikhilAgrawal, you could have your function return right after finish() is called.Millur
Does finish() also call onPause() and onStop() before calling onDestroy() ?Skippy
@user3282164: If the activity is not paused and stopped already, yes.Radome
@Radome I am performing same way but Activity can not finish.#40233922Hemelytron
A
21

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.

Algophobia answered 1/1, 2015 at 14:31 Comment(0)
K
13

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.

Knot answered 7/4, 2010 at 8:38 Comment(0)
B
1

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.
Blueness answered 31/7, 2014 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.