Synchronous and asynchronous activities

后端 未结 1 718
我在风中等你
我在风中等你 2021-02-05 16:53

Can anyone help me to understand synchronous and asynchronous activities in Android?

What is exactly meant by synchronous and asynchron

相关标签:
1条回答
  • 2021-02-05 17:17

    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)

    0 讨论(0)
提交回复
热议问题