Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the mechanism behind startActivityForResult() in Android?

I have an activity. In this activity I want to start another activity using startActivityForResult(). I understand that my basic activity is started within a process with a main GUI thread. But as far as I understand, startActivityForResult() is asynchronious which means that my new activity will be executed in a different thread. I can't find information regarding the threads inside. If there is only one GUI thread, how does these functions work asynchroniously ?

like image 521
Patric Avatar asked Aug 15 '10 20:08

Patric


People also ask

What is purpose of startActivityForResult () function?

By the help of android startActivityForResult() method, we can send information from one activity to another and vice-versa. The android startActivityForResult method, requires a result from the second activity (activity to be invoked).

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.

What is use instead of startActivityForResult in Android?

In place of startActivityForResult(intent, 123), use Intent intent = new Intent(this, SampleActivity.

How do you manage 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 .


1 Answers

But as far as I understand, startActivityForResult() is asynchronious which means that my new activity will be executed in a different thread.

startActivityForResult() is asynchronous. That does not mean that your new activity will be executed in a different thread. If the new activity is part of your own application, it runs on the main application thread, like all your other activities.

If there is only one GUI thread, how does these functions work asynchroniously ?

startActivityForResult(), like startActivity(), does not do anything immediately. Rather, it puts a message on a message queue, then returns. When you return control back to Android (e.g., your onClick() method ends), Android goes back to processing messages off of that queue. When it gets to your start-activity message, it starts up the new activity.

like image 126
CommonsWare Avatar answered Sep 29 '22 22:09

CommonsWare