如何阻止 Android 7.1 应用快捷方式忽略活动启动模式



我有一个静态的应用程序快捷方式,声明如下:

<shortcut
    android:enabled="true"
    android:icon="@drawable/shortcut"
    android:shortcutDisabledMessage="@string/downloads"
    android:shortcutId="downloads"
    android:shortcutLongLabel="@string/downloads"
    android:shortcutShortLabel="@string/downloads">
    <intent
        android:action="android.intent.action.VIEW"
        android:targetClass="com.colinrtwhite.test.activity.DownloadsActivity"
        android:targetPackage="com.colinrtwhite.test"/>
</shortcut>

它在我的 Android 清单中声明.xml如下所示:

<activity
    android:name=".activity.DownloadsActivity"
    android:launchMode="singleTask"
    android:theme="@style/AppTheme"/>

根据文档,singleTask启动模式应该重用活动的现有实例,并通过 onNewIntent 方法传递新意图。但是,如果我有一个现有的下载活动实例并点击应用程序快捷方式以启动它,它将销毁然后重新创建活动。

我的问题:如何强制应用快捷方式重复使用我的活动的现有实例而不重新启动它?

根据文档,您正在使用静态快捷方式

静态快捷键不能具有自定义 Intent 标志。静态快捷方式的第一个目的将始终设置FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TASK。这意味着,当应用程序已在运行时,启动静态快捷方式时,所有现有活动都将被销毁。

也根据同一部分

可以使用任何一组 Intent 标志发布动态快捷方式。通常,指定FLAG_ACTIVITY_CLEAR_TASK,可能与其他标志一起指定;

https://developer.android.com/reference/android/content/pm/ShortcutManager.html,部分快捷方式意图。

最新更新