其他分享
首页 > 其他分享> > android – 具有singleTask启动模式的活动及其在后台堆栈中的位置

android – 具有singleTask启动模式的活动及其在后台堆栈中的位置

作者:互联网

在官方在线Android guide on tasks and back stack中,有一条我发现自相矛盾的信息.如果有人能帮助我解决这个问题,我将不胜感激.

首先,有以下声明:

Activities in the stack are never rearranged, only pushed and popped from the stack—pushed onto the stack when started by the current activity and popped off when the user leaves it using the Back button.

然后描述了singleTask启动模式,有以下声明:

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance.

好吧,如果我理解正确的话,singleTask活动只能是他们任务的根源,因为它们是以这种方式创建的,并且活动永远不会在后台堆栈中交换位置.但是这样的活动如何在接收对onNewIntent()方法的调用时做出反应呢?

在该页面上有关于此事的另一个评论:

As another example, the Android Browser application declares that the web browser activity should always open in its own task—by specifying the singleTask launch mode in the element. This means that if your application issues an intent to open the Android Browser, its activity is not placed in the same task as your application. Instead, either a new task starts for the Browser or, if the Browser already has a task running in the background, that task is brought forward to handle the new intent.

因此,该指南指出,调用singleTask活动会以某种方式使其前进.但是,如果这项活动不是最重要的,那怎么可能呢?活动是否已恢复,或者只是在没有向用户显示的情况下接收对其onNewIntent()方法的调用?我真的不明白.

PS:一年多前已经问过几乎相同的question,但没有给出正确答案.因此,虽然从技术上讲,我的问题是重复的,但我觉得有必要重新提出它,因为它确实值得一个明确而明确的答案.

解决方法:

我会尽力回答你的问题.

首先,

Activities in the stack are never rearranged, only pushed and popped
from the stack—pushed onto the stack when started by the current
activity and popped off when the user leaves it using the Back button.

这实际上是错误的.通常,堆栈中的活动永远不会重新排列,但您可以使用Intent.FLAG_ACTIVITY_REORDER_TO_FRONT将现有活动从堆栈中的其他位置带到前面.例如,您的活动堆栈如下所示:A-> B-> C-> D其中A是根活动(位于堆栈底部),D是前景中最顶层的活动(是示出).如果D现在这样做:

Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

这会将活动B的现有实例移动到任务的前面,这样它就不会显示在屏幕上.活动堆栈现在看起来像这样:A-> C-> D-> B.

接下来你问这个:

The system creates a new task and instantiates the activity at the
root of the new task. However, if an instance of the activity already
exists in a separate task, the system routes the intent to the
existing instance through a call to its onNewIntent() method, rather
than creating a new instance.

好吧,如果我理解正确的话,singleTask活动只能是他们任务的根源,因为它们是以这种方式创建的,并且活动永远不会在后台堆栈中交换位置.但是这样的活动如何在接收对onNewIntent()方法的调用时做出反应呢?

通常,singleTask活动是其任务的根.如果已经有一个现有任务激活了singleTask活动,并且您使用startActivity()启动了这样的活动,那么该任务将被提前(以便在屏幕上显示)并且onNewIntent()将在该活动的现有实例.但是,如果singleTask活动已在其任务中启动了其他活动,则所有其他活动都将完成(即:任务被剥离回其根Activity)BEFORE onNewIntent()将在现有的singleTask活动实例上调用.

有些情况下,singleTask活动不是其任务的根,然后所有的投注都是关闭的(即:行为不是记录的).如果singleTask活动与应用程序中已在任务中处于活动状态的其他活动具有相同的taskAffinity,则会发生这种情况.在这种情况下,Android会忽略singleTask启动模式,并将活动视为具有标准启动模式.

标签:android,android-activity,android-intent,back-stack,launchmode
来源: https://codeday.me/bug/20190628/1315958.html