gpt4 book ai didi

Android FLAG_ACTIVITY_NEW_TASK 被忽略

转载 作者:太空宇宙 更新时间:2023-11-03 12:39:59 25 4
gpt4 key购买 nike

是否有忽略 FLAG_ACTIVITY_NEW_TASK 的情况?我从一个服务开始一个 Activity ,其 Intent 是设置该标志,当主要 Activity 在后台时,我希望在新任务中看到新 Activity ,所以我希望在后台再次看到应用程序,当我关闭新 Activity 。它不会发生,并且会显示主要 Activity 。

只是为了测试,我尝试将主要 Activity 设置为 singleInstance,在这种情况下它可以工作。不过,我不想将主要 Activity 设置为 singleInstance。

你知道这什么时候会发生吗?

我应该补充一点,我在所有设备上都看到了这种行为,所以看起来这是它应该工作的方式,而不是设备特定的问题。

最佳答案

我修复了它,为第二个 Activity 设置了不同的亲和性。问题是有关 FLAG_ACTIVITY_NEW_TASK 标志的文档指出:

If set, this activity will become the start of a new task on this history stack

但这具有误导性,因为听起来框架总是会创建一个新任务,而当 Activity 从服务启动时情况并非如此。

这是关于 Activity 的 taskaffinity 属性的文档:

android:taskAffinity The task that the activity has an affinity for. Activities with the same affinity conceptually belong to the same task (to the same "application" from the user's perspective). The affinity of a task is determined by the affinity of its root activity. The affinity determines two things — the task that the activity is re-parented to (see the allowTaskReparenting attribute) and the task that will house the activity when it is launched with the FLAG_ACTIVITY_NEW_TASK flag.

因此,如果您从 Activity 中调用 startActivity,那么会发生一个新任务,但是如果您从服务中启动它,则框架会将 Activity 附加到具有相同亲和性的任务(如果顶级 Activity 存在的话)那不是一个单一的实例

关于Android FLAG_ACTIVITY_NEW_TASK 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10301902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com