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 ?
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).
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.
In place of startActivityForResult(intent, 123), use Intent intent = new Intent(this, SampleActivity.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With