Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone please explain how startActivity(intent) and startActivityForResult(intent) are Asynchronous?

Tags:

If an Asynchronous thread is a thread that operates separately to the main thread and doesn't interfere with the main thread...

Does a new Activity not occupy the main thread after it has been started through startActivity(intent)?

The majority of what I have read on this says these are both asynchronous, however there are a fair few conflicting answers and the people that say this don't really give convincing arguments.

So if anyone who has this clear in their head and could explain why they are synchronous/asynchronous, I would be a very grateful man!

Cheers

EDIT: So the answer I have derived from these two good folk and some stuff online...

Bringing Activities into the mix of synchronous/asynchronous can cause a load of horse to come about. But it is still referring to the principles of...

Synchronous methods/commands must be completed before the call stack can continue.

Asynchronous methods/commands (commonly done in a thread/AsyncTask) continue in parallel allowing the main thread of execution to continue. (It can return in its own time)

The startActivity(intent) and startActivityForResult(intent) methods are Asynchronous as they are non-blocking and allow the thread of execution to continue whilst performing their corresponding task as well.

like image 527
mgibson Avatar asked Dec 17 '12 17:12

mgibson


People also ask

Is intent asynchronous?

Intents are asynchronous messages which allow application components to request functionality from other Android components.

What is startActivityForResult?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.

What is request code startActivityForResult?

The request code is any int value. The request code identifies the return result when the result arrives. ( You can call startActivityForResult more than once before you get any results. When results arrive, you use the request code to distinguish one result from another.

How do I use startActivityForResult?

First you use startActivityForResult() with parameters in the first Activity and if you want to send data from the second Activity to first Activity then pass the value using Intent with the setResult() method and get that data inside the onActivityResult() method in the first Activity .


2 Answers

startActivity(intent) and startActivityForResult(intent) are asynchronous in the sense that these methods return immediately without starting an Activity. Actually, they schedule an Activity to start only after the lifecycle events of the current Activity is finished.

The takeaway is, if you have something, that takes some time to finish, in the onPause() method of the first activity , the new Activity will be slow to start.

like image 64
Vasudev Avatar answered Sep 22 '22 01:09

Vasudev


When you startActivityForResult you still perform an asynchronous call. Your caller activity gets suspended and the new is started in another process (if it runs under a different user).

But when the called activity terminates setting a result, your activity is resumed and you get onActivityResult called as a callback containing the result.

like image 20
usr-local-ΕΨΗΕΛΩΝ Avatar answered Sep 21 '22 01:09

usr-local-ΕΨΗΕΛΩΝ