【问题标题】:How to stop Android 7.1 app shortcuts from ignoring Activity launchMode?如何阻止 Android 7.1 应用程序快捷方式忽略 Activity 启动模式?
【发布时间】:2017-03-21 21:02:18
【问题描述】:

我有一个这样声明的静态应用快捷方式:

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

According to the documentationsingleTask 启动模式应该重用 Activity 的现有实例并通过 onNewIntent 方法传递新的意图。但是,如果我有一个现有的 DownloadsActivity 实例并点击应用快捷方式来启动它,它将销毁然后重新创建 Activity。

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

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

    也按同一部分

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

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

    【讨论】:

    • 嗯,所以看起来使用动态快捷方式可能会解决我的问题。今晚晚些时候我会试一试并报告(如果它解决了问题,请接受你的回答)。谢谢!
    猜你喜欢
    • 2012-06-14
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多