How does Activity.finish() work in Android?

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.


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.

Tags:

Java

Android