13

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60
  • @Dinesh..Your question is same as [link] http://stackoverflow.com/questions/8613295/difference-between-synchronous-activities-ans-asynchronous-activities[link] and also you have replied on this post. So why you are asking the same question.Moreover AFAIK startSubActivity is never released in public and it's functionality is provided by startActivityForResult() – Maverick Jan 06 '12 at 18:40
  • Thanks Ravi for the comment. I replied that question but i m still little confused over this concept. So want to be 100% clear. If you have some Idea over this than you can share with me ...:) – Dinesh Sharma Jan 06 '12 at 18:58
  • 1
    "Can anyone help me to explain Synchronous and ASynchronous Activity in android." -- there is no such concept in Android. – CommonsWare Jan 06 '12 at 19:22
  • @CommonsWare : what ever the user is doing in an activity - there is a UI thread which is running, at-least, so according to you an activity is a process or just collection of few threads out of a process for the Application? – Bhuro Sep 05 '16 at 10:53
  • @Bhuro: An activity is a Java class. It is neither a process nor a collection of a few threads. – CommonsWare Sep 05 '16 at 10:57
  • ok, @CommonsWare I mean at the time of executing by system ... ?? As it is a Java class, there mill be an object, the object is performing any function... as a part of process, or thread ??? – Bhuro Sep 05 '16 at 11:14

1 Answers1

18

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)

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Bruno Oliveira
  • 5,056
  • 18
  • 24
  • 1
    Thanks Bruno for a quick reply. But can just explain me in what sense the term "synchronous" and "asynchronous" are used for as I have seen in many places these type of question arises.... – Dinesh Sharma Jan 06 '12 at 19:05
  • 3
    Synchronous means that execution will not continue until a certain task is finished. If `startActivity` worked this way, all the interface would freeze until the other activity was running. Asynchronous means that your task will take place somewhere in the future, and execution continues in the meanwhile: you request that an Activity be started, and your code continues to execute knowing that it will eventually happen. – salezica Jan 07 '12 at 14:57
  • 1
    The answer is not addressing the question properly. – Muhammad Babar Sep 28 '14 at 12:40
  • What is the use of `setResult` ? It only returns void and only takes requestCode and an `Intent?` object as parameter. How would you pass the data using `setResult` ? – NullByte08 Oct 16 '20 at 12:42
  • I am simply creating my own functions. And calling those functions inside the onActivityResult() and passing the data processed in the onActivityResult() as parameters to the functions. I dont find any use case for `setResult` – NullByte08 Oct 16 '20 at 12:46