Synchronous and asynchronous activities

拜拜、爱过 提交于 2019-12-04 10:37:09

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/8762330/synchronous-and-asynchronous-activities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!