我有一个包含 3 个 Activity 的示例应用程序:Main、A 和 B。它们都没有任何启动模式。
现在我这样做:
- 打开应用,显示主要 Activity
- 使用后退按钮离开应用程序,这样就没有正在运行的 Activity
- 接收启动新 Activity A 的广播(new_task 标志)
- 通过单击 Activity A 中的按钮打开 Activity B(无标志)
- 接收启动新 Activity A 的广播(new_task 标志)
- 新 Activity A 未开始 (如果我回去,还有之前的Activity A)
在第 6 步, Activity A 应该显示但没有显示。
如果我尝试展示 Activity C(在 #5 中),它会按预期呈现。
如果在 #2 中我改为通过主页按钮离开应用程序,一切都会按预期进行。
这怎么可能?我如何确保 Activity 始终呈现?我可以使用 FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP
,但我想维护后台堆栈。
更新 - 来源: 你可以看看source ,但我使用的是 Xamarin(它基本上是用 C# 编写的原生 Android,与 Java 非常相似)。 这是构建 list 的内容(删除了 Xamarin 内容):
<?xml version="1.0" encoding="utf-8" standalone="no"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" android:compileSdkVersion="28" android:compileSdkVersionCodename="9" package="com.companyname.StartTest" platformBuildVersionCode="28" platformBuildVersionName="9">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application android:allowBackup="true" android:debuggable="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:name="android.app.Application" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:label="A" android:name="A"/>
<activity android:label="B" android:name="B"/>
<activity android:label="@string/app_name" android:name="MainActivity" android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name="Receiver"/>
</application>
</manifest>
最佳答案
好吧,让我们按照操作顺序来看这个。
打开应用,显示主要 Activity
后台状态:
Main Activity (root)
使用后退按钮离开应用程序,这样就没有正在运行的 Activity
后台状态:
<No activity present as back was pressed>
接收启动新 Activity A 的广播(new_task 标志)
后台状态:
ActivityA (root)
通过点击 Activity A 中的按钮打开 Activity B(无标志)
后台状态:
ActivityA (root) -> ActivityB (top)
接收启动新 Activity A 的广播(new_task 标志)
后台状态:
ActivityA (root) -> ActivityB (top)
此处 Actvitity A 未启动,因为它是使用具有以下文档的 Intent 标志“FLAG_ACTIVITY_NEW_TASK”启动的:
When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in.
- 新的Activity A没有开始(如果我回去,还有之前的Activity A)
在第 6 步, Activity A 应该显示但没有显示。
So the current task looks like this
ActivityA (root) -> ActivityB(top)
,ActivityA
is already present in the task and so the task as a whole is just brought to the front which meansActivityB
is still at the top of the task.
如果我尝试展示 Activity C(在 #5 中),它会按预期呈现。
ActivityC
is a new activity that is not there in the task and so it is created for you.
如果在 #2 中我改为通过主页按钮离开应用程序,一切都会按预期进行。
关于按下主页按钮的事情是,在这种情况下 MainActivity 没有被销毁,只是被推到后台。
所以在#2 之后,你的 backstack 状态看起来仍然有 MainActivity 实例作为根,比如:
Main Activity (root)
现在,在#5 之前,backstack 看起来像这样:
Main Activity (root) -> ActivityA -> ActivityB (top)
在#5 之后,你的后栈看起来像这样:
Main Activity (root) -> ActivityA -> ActivityB -> ActivityA (top)
我认为原因似乎是 MainActivity
的方式默认情况下使用 Intent 标志启动:
FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
这也会触发任务重新设置父级,因此它可以控制使用新任务标志创建的新 Activity (与父应用程序具有相同的亲缘关系)以及如何将此类 Activity 移动到其任务。
您可以找到有关此 here 的更多信息
我如何确保 Activity 始终呈现?我可以使用 FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP,但我想维护后台堆栈。
由于这是一个堆栈数据结构,您无法重新排序您的 Activity ,但是您可以通过添加标志来解决此问题:FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_MULTIPLE_TASK
在创建 ActivityA 的接收器中,因为这将确保为 Activity 创建一个新的实例/任务,无论它是否已经存在。 (不是推荐的解决方案
- 但有效)
但是如果你想维护 ActivityA
的同一个实例那么您必须向 list 中的 Activity 添加一个 taskAffinity 参数。
<activity android:name=".ActivityB" android:taskAffinity="com.example.pendingintent"></activity>
<activity android:name=".ActivityA" android:taskAffinity="com.example.abctest" />
然后使用 new_task 标志启动 Activity A 和 B
Here是一个很好的幻灯片,解释了 taskAffinity 以及它如何影响 Activity 的创建。
关于如果已经作为根目录在后台堆栈中,Android Activity 未启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56466769/