如何阻止Android 7.1应用程序快捷方式忽略Activity launchMode?

3

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

<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>

它在我的AndroidManifest.xml中声明如下:

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

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

我的问题是:如何强制应用程序快捷方式重用我Activity的现有实例而不是重新启动它?

1个回答

5
您正在使用静态快捷方式,根据文档。
静态快捷方式不能具有自定义意图标志。静态快捷方式的第一个意图始终会设置FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TASK。这意味着,当应用程序已经在运行时,所有现有活动将在启动静态快捷方式时被销毁。
同样根据相同部分
动态快捷方式可以使用任何一组Intent标志进行发布。通常指定FLAG_ACTIVITY_CLEAR_TASK,可能还包括其他标志; https://developer.android.com/reference/android/content/pm/ShortcutManager.html,部分快捷方式意图。

嗯,看起来使用动态快捷方式可能会解决我的问题。我今晚稍后会尝试一下并回报(如果它解决了问题,我会接受你的答案)。谢谢! - Colin White

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接