Application Fundamentals--Starting tasks

Starting tasks

An activity is set up as the entry point for a task by giving it an intent filter with "android.intent.action.MAIN" as the specified action and "android.intent.category.LAUNCHER" as the specified category. (There's an example of this type of filter in the earlier Intent Filters section.) A filter of this kind causes an icon and label for the activity to be displayed in the application launcher, giving users a way both to launch the task and to return to it at any time after it has been launched.

翻译:把一个Activity设置为一个task的起点的办法是在manifest文件中的这个Activity元素的intent filter子元素的action属性设定为 "android.intent.action.MAIN" 的同时把它的category属性设定为"android.intent.category.LAUNCHER",这样设置的过滤器会使得对应的Activity在系统的应用程序发射台界面中显示一个图标和说明文字,这样一来,用户可以在系统的应用程序发射台界面中选中这个图标来启动这个task,而且用户也可以在task启动后还可以随时返回该task。

This second ability is important: Users must be able to leave a task and then come back to it later. For this reason, the two launch modes that mark activities as always initiating a task, "singleTask" and "singleInstance", should be used only when the activity has a MAIN and LAUNCHER filter. Imagine, for example, what could happen if the filter is missing: An intent launches a "singleTask" activity, initiating a new task, and the user spends some time working in that task. The user then presses the HOME key. The task is now ordered behind and obscured by the home screen. And, because it is not represented in the application launcher, the user has no way to return to it.

翻译:能使得用户可以在一个task启动后随时能返回该task的这个功能很重要:因为这个功能也是用户实际使用过程很需要的,所以,action属性是 "android.intent.action.MAIN" 和category属性是"android.intent.category.LAUNCHER"的Activity的启动模式只能是"singleTask" 或 "singleInstance"这两个启动模式。可以想象一下,假如没有设定这两个属性值的话,将会发生什么问题:一旦一个intent请求对象启动了一个"singleTask"启动模式的activity后,系统将启动一个新的task,用户基于这个task花了很长时间做了相关的操作,然后,用户按下HOME键,这时候该task退到后台,从主屏幕中消失,由于该Activity没有同时设定它的过滤器元素的action属性设定为 "android.intent.action.MAIN" 、category属性设定为"android.intent.category.LAUNCHER",所以在应用发射台上是没有相应的启动图标的,那么就直接导致用户无法重新回到该task。

A similar difficulty attends the FLAG_ACTIVITY_NEW_TASK flag. If this flag causes an activity to begin a new task and the user presses the HOME key to leave it, there must be some way for the user to navigate back to it again. Some entities (such as the notification manager) always start activities in an external task, never as part of their own, so they always put FLAG_ACTIVITY_NEW_TASK in the intents they pass to startActivity(). If you have an activity that can be invoked by an external entity that might use this flag, take care that the user has a independent way to get back to the task that's started.

翻译:同样的问题也会在使用FLAG_ACTIVITY_NEW_TASK flag的时候遇到,如果一个intent的这个flag导致Activity启动一个新的task,然后用户按下HOME键离开当前task,必须能让用户还能返回该task。有些实体如notification manager通常是以外部task方式启动Activity的,这些实体总是先在一个intent中设定FLAG_ACTIVITY_NEW_TASK,然后执行startActivity()方法启动某个Activity。如果你的Activity也是这样的方式被实体调用并启动的话,需要特别注意你的应用需要提供其他的途径能让用户能够重新返回到这个task。

For those cases where you don't want the user to be able to return to an activity, set the <activity> element's finishOnTaskLaunch to "true". See Clearing the stack, earlier.

翻译:某些场合你可能不希望用户返回到task堆栈中的某个Activity对应的界面,那么可以把这个Activity的启动模式finishOnTaskLaunch 设定为 "true",具体请查阅之前的章节:Clearing the stack
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值