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

标签 android android-intent android-activity back-stack launchmode

在安卓官网上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 Activity 只能是它们任务的根,因为它们是以这种方式创建的,并且 Activity 从不交换后台堆栈中的位置。但是这样的 Activity 在收到对其 onNewIntent() 方法的调用时会如何 react ?

那个页面上还有一个关于此事的评论:

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 Activity 以某种方式将其提前。但是,如果该 Activity 不在其任务之上,那怎么可能是真的呢? Activity 是否已恢复,或者它只是收到对其 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.

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

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

这会将 Activity B 的现有实例移动到任务的前面,这样它就不会显示在屏幕上。 Activity 堆栈现在将如下所示: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 Activity 只能是它们任务的根,因为它们是以这种方式创建的,并且 Activity 永远不会在后台堆栈中交换位置。但是,此类 Activity 在收到对其 onNewIntent() 方法的调用时会如何 react ?

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

在某些情况下,singleTask Activity 不是其任务的根,然后所有的赌注都没有了(即:行为没有记录)。如果 singleTask Activity 与应用中已在任务中处于 Activity 状态的其他 Activity 具有相同的 taskAffinity,则可能会发生这种情况。在这种情况下,Android 会忽略 singleTask 启动模式,并将 Activity 视为具有 standard 启动模式。

关于android - 具有 singleTask 启动模式的 Activity 及其在后台堆栈中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31575072/

相关文章:

android - 从 Android 'Assets' 文件夹访问 CSS 字体

android - 旋转设备时 EditText 数据丢失

java - AlertDialog 中的 Android TabActivity

android - 搜索栏十进制增量值

android - LocalBroadcastManager 未在 Activity 中接收广播

java - fragment java.lang.ClassCastException错误

java - Android - 通过ScrollView的一部分传递触摸事件

android - firebase 和 google play 服务无法解决异常

Android:如何从 Activity 到 Service 到第二个 Activity

java - 答案正确时不正确 Android 测验应用程序