同步和异步活动 [英] Synchronous and asynchronous activities

查看:85
本文介绍了同步和异步活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我了解Android中的同步异步活动吗?

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

Android中的同步和异步活动到底是什么?

What is exactly meant by synchronous and asynchronous activity in Android?

StartActivityStartSubActivityStartAcivityForResult是同步还是异步启动活动,或者它们可以同时以两种方式运行吗?

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.

推荐答案

首先,在Android上一次只能运行一个活动,因此您永远不会同时运行两个活动.当您想解雇"时,即要启动活动但不希望它为活动返回值时,请使用startActivity().在这种情况下,新活动将开始,您的活动将被暂停;用户返回到您的活动后,您最终可能会重新获得控制权.

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.

从正在启动的活动中获取结果时,请使用startActivityForResult().在这种情况下,调用活动应覆盖onActivityResult(),当启动的活动退出并有返回结果(由setResult()设置)时,该活动将被调用.

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()).

在这两种情况下,由于调用活动和被调用活动在相同的 task 中,因此在某种意义上是同步的"(尽管我认为使用术语同步"和异步"在这种情况下可能会造成混淆).呼叫活动完成之前,呼叫活动不会出现在屏幕上.

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.

了解更多信息的有用读物是: * http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html

A useful read to know more is: * http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html

- Bruno Oliveira(Android开发者关系,Google)

- Bruno Oliveira (Android Developer Relations, Google)

这篇关于同步和异步活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆