微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – launchMode =“singleTask”不会创建新任务

我有2个活动:活动A和活动B.活动A的启动模式是标准的,活动B的启动模式是singleTask.
<activity
    android:name=".AActivity"
    android:label="@string/app_name"
    android:launchMode="standard">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="dd"></data>
        <data android:host="a"></data>
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.broWSABLE" />
    </intent-filter>
</activity>
<activity
    android:name=".BActivity"
    android:label="@string/app_name"
    android:launchMode="singleTask">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="dd"></data>
        <data android:host="b"></data>
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.broWSABLE" />
    </intent-filter>
</activity>

我启动了应用程序,启动器启动了活动A.然后我按下主页按钮并返回到手机的主屏幕.然后我启动浏览器应用程序并输入以下内容

dd://b

打开活动B.系统导航到我的应用程序并在活动A之上启动活动B.此时,如果我按回按钮,活动B将弹出,我看到活动A.

这不是我所期望的,因为android文档说明:

For singleTask activities,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. Only one instance of the activity can exist at a time.

我从这些句子中理解的是,在我的情况下,因为活动B的实例不存在,所以应该启动一个新任务,它应该只有活动B在它的堆栈(我的应用程序的另一个实例应该仍然存在于单独的任务,它的堆栈中应该有活动A.然后,如果我在活动B时按回,因为它是后台堆栈中的唯一活动,它会弹出并且系统返回到浏览器.

为什么不是这样的? android系统如何知道我的应用程序是打开并导航到它并在现有应用程序堆栈上启动活动B而不是启动我的应用程序的另一个实例并让我的应用程序的两个实例拥有自己的堆栈?在新任务中实例化活动意味着什么?谁能解释一下?

谢谢.

解决方法

除了接受的答案,我找到了解决这个问题的方法.正如android文档所述:

The affinity indicates which task an activity prefers to belong to. By default,all the activities from the same application have an affinity for each other. So,by default,all activities in the same application prefer to be in the same task which belongs to that app(The app that has the Activity B). However,you can modify the default affinity for an activity.

因此,如果您的应用程序正在运行,并且您启动了具有launchMode:singleTask属性的活动,除非您明确声明了taskAffinity属性,它将在同一任务中启动活动.但是,如果您在清单中明确说明了亲和力:

<activity
        android:name=".BActivity"
        android:label="@string/app_name"
        android:taskAffinity=""
        android:launchMode="singleTask">
</activity>

然后它在一个新任务中启动活动.

您可以通过以下adb命令查看哪个活动属于哪个任务:

adb shell dumpsys activity

此外,为了更好地了解launchMode概念,您可以在Google Play中看到以下应用:https://play.google.com/store/apps/details?id=com.novoda.demos.activitylaunchmode

原文地址:https://www.jb51.cc/android/308656.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐