本文介绍了安卓:行为launchMode =&QUOT的; singleTask"与描述不符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习Android和已经遇到一个问题,launchMode =singleTask。该文件指出,当这个属性时,该活动一直开展到一个新的任务,因为根系活力。其次,该文件指出,如果目的是针对这样的时候有活动坐在它上面的任务堆栈的活动,这样的意图被丢弃(虽然任务仍置于前台)。

I've been learning Android and have come across an issue with launchMode="singleTask". The documentation states that when this attribute is used, the Activity is always launched into a new task as the root Activity. Secondly, the documentation states that if an Intent is targeted at such an Activity when there are Activities sitting above it in its task stack, such Intents are discarded (although the task is still brought to the foreground).

我一直在玩弄这一点,我观察到的行为是完全不同的。尤其是: - 与launchMode =singleTask活动并不总是在任务堆栈中的根系活力。他们只是plonked ontop具有相同的亲和力现有的堆栈。 - 当一个目的是针对这样的活动,并有其他活动在它上面的堆栈,目的是不被丢弃。相反,它上面堆栈中的活动将被丢弃。的意图然后经由onNewIntent递送到活性为正常。

I've been playing around with this, and the behaviour I observe is completely different. In particular:- Activities with launchMode="singleTask" are not always the root Activity in a task stack. They are just plonked ontop of the existing stack with the same affinity.- When an Intent is targeted at such an Activity and there are other Activities above it in the stack, the Intent is not discarded. Instead the Activities above it in the stack are discarded. The Intent is then delivered via onNewIntent to the Activity as normal.

有人可以证实,这是实际的行为吗?如果是这样,为什么这些文件不正确的?如果不是我做了什么错。 。

Can someone confirm that this is the actual behaviour? If so, why are the documents incorrect? If not what have I done wrong. . .

编辑:

本例展示了这种行为可以在这里找到:http://groups.google.co.uk/group/android-developers/browse_thread/thread/fd8f249803b4688a#对不起,跨岗位,但我无法得到一个体面的格式的例子,张贴在这里...

An example demonstrating this behaviour can be found here:http://groups.google.co.uk/group/android-developers/browse_thread/thread/fd8f249803b4688a#Sorry for the cross-post, however I was unable to get the example in a decent format to post here...

推荐答案

这是 taskAffinity 的问题。当你开始一个活动,安卓检查要启动活动的 taskAffinity 。如果是一样的 taskAffinity 在任务根系活力,那么它就会忽略 launchMode =singleInstance launchMode =singleTask (因为这些发射模式需要机器人来创建一个新的任务发起的活动),并开始活动在当前的任务。

This is a problem of taskAffinity. When you start an activity, Android checks the taskAffinity of the activity that you want to start. If it is the same as the taskAffinity of the root activity in your task, then it will ignore launchMode="singleInstance" or launchMode="singleTask" (because those launch modes would require Android to create a new task to launch the activity in) and start the activity in the current task.

不幸的是,这不是有据可查的,但 taskAffinity 花费precedence了 launchMode

Unfortunately, this isn't well documented, but taskAffinity takes precedence over launchMode.

如果你真的想要一个 singleTask singleInstance 活动(通常是不应该做的事情,因为它用它,你可能会得到错误的其他讨厌的东西一整个混乱带来的),那么你需要确保你的 singleInstance singleTask 的活动已经在体现在以下的<活性GT; 定义:

If you really want a singleTask or singleInstance activity (which is usually not the right thing to do because it brings with it a whole mess of other nasty things that you are likely to get wrong), then you need to make sure that your singleInstance or singleTask activity has the following in the manifest in its <activity> definition:

android:taskAffinity=""

这篇关于安卓:行为launchMode =&QUOT的; singleTask&QUOT;与描述不符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:18