Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronous and asynchronous activities

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.

like image 614
Dinesh Sharma Avatar asked Jan 06 '12 18:01

Dinesh Sharma


1 Answers

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)

like image 95
Bruno Oliveira Avatar answered Oct 19 '22 12:10

Bruno Oliveira