Can anyone help me to understand synchronous and asynchronous activities in Android?
What is exactly meant by synchronous and asynchronous activity in Android?
StartActivity
, StartSubActivity
and StartAcivityForResult
start an activity synchronously or asynchronously, or can they behave in both ways?
Please explain as I have gone through many articles but could not find any proper explaination over this.
First of all, only one activity can be running at a time on Android, so you'll never have two activities running at the same time. Use startActivity()
when you want to "fire and forget", that is, you want to launch an activity but are not expecting it to return a value to your activity. In that case, the new activity will start and your activity will be paused; you might eventually regain control once the user returns to your activity.
Use startActivityForResult()
when you are expecing a result from the activity you are launching. In this case, the calling activity should override onActivityResult()
, which will be called when the launched activity exits and has a result to return to you (which it sets with setResult()
).
In both cases, since the calling activity and the called activity are in the same task, it's "synchronous" in a certain sense (although I think using the terms "synchronous" and "asynchronous" can be confusing in this context). The calling activity won't appear on the screen until the called activity finishes.
A useful read to know more is: * http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
- Bruno Oliveira (Android Developer Relations, Google)
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