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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...