Android 动态快捷方式无法正确打开 activity 第二次

Android dynamic shortcuts not opening proper activity second time

您好,我正在为 android 应用程序开发快捷方式。我的代码看起来像

private void setShortMenu() {

    ArrayList<ShortcutInfo> shortcutInfos = new ArrayList<>();

    Intent intent = new Intent(this, SampleActivity2.class);
    intent.setAction(Intent.ACTION_VIEW);
    intent.putExtra("val",1);
    intent.addCategory("android.shortcut.conversation");
    ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

    ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id2")
            .setShortLabel("Web site")
            .setLongLabel("Open the web site")
            .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
            .setIntent(intent)
            .build();
    shortcutInfos.add(shortcut);

    Intent intent2 = new Intent(this, SampleActivity3.class);
    intent2.setAction(Intent.ACTION_VIEW);
    intent.addCategory("android.shortcut.conversation");
    intent.putExtra("val",2);

    ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this, "id1")
            .setShortLabel("Web site...")
            .setLongLabel("Open the web site...")
            .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
            .setIntent(intent2)
            .build();
    shortcutInfos.add(shortcut2);

    shortcutManager.setDynamicShortcuts(shortcutInfos);
}

我的清单如下所示:

<application
    android:name=".SampleApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
            <!--<meta-data android:name="android.app.shortcuts"
            android:resource="@xml/shortcuts" />-->
    </activity>
    <activity android:name=".SampleActivity" />
    <activity android:name=".SampleActivity2" />
    <activity android:name=".SampleActivity3"></activity>

这段代码的问题是这样的。我从后台杀死了应用程序。单击第一个快捷方式。它打开 SampleActivity2。我通过单击主页按钮最小化应用程序并单击第二个快捷方式。它打开 SampleActivity3。到目前为止,一切都是正确的。但现在如果我点击第一个菜单或第二个菜单,它总是打开 SampleActivity3,最后最小化 activity。 如果我用静态快捷方式做同样的事情那么它工作正常:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
    android:shortcutId="compose"
    android:enabled="true"
    android:icon="@mipmap/ic_launcher"
    android:shortcutShortLabel="@string/compose_shortcut_short_label1"
    android:shortcutLongLabel="@string/compose_shortcut_short_label1"
    android:shortcutDisabledMessage="@string/compose_shortcut_short_label1">
    <intent
        android:action="android.intent.action.VIEW"
        android:targetPackage="com.example.nilkash.shortcutmenu"
        android:targetClass="com.example.nilkash.shortcutmenu.SampleActivity2" >
        <extra android:name="val" android:value="1" />
    </intent>
    <categories android:name="android.shortcut.conversation" />
</shortcut>

<shortcut
    android:shortcutId="compose1"
    android:enabled="true"
    android:icon="@mipmap/ic_launcher"
    android:shortcutShortLabel="@string/compose_shortcut_short_label2"
    android:shortcutLongLabel="@string/compose_shortcut_short_label2"
    android:shortcutDisabledMessage="@string/compose_shortcut_short_label2">
    <intent
        android:action="android.intent.action.VIEW"
        android:targetPackage="com.example.nilkash.shortcutmenu"
        android:targetClass="com.example.nilkash.shortcutmenu.SampleActivity3" >
        <extra android:name="val" android:value="2" />
    </intent>
    <categories android:name="android.shortcut.conversation" />
</shortcut>

我在清单中添加了:

<activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
            <meta-data android:name="android.app.shortcuts"
            android:resource="@xml/shortcuts" />
    </activity>

我的代码有什么问题吗?需要一些帮助。谢谢。

这可以在你 post 你的清单后得到确认,但我猜发生了以下情况:

当您使用第一个快捷方式时,Android 会为您的应用程序创建一个新任务并在该任务中启动 SampleActivity2。然后你按 HOME,这只会将这个任务发送到后台。

现在您使用第二个快捷方式。由于 SampleActivity3SampleActivity2 具有相同的 taskAffinity,因此 Android 将包含 SampleActivity2 的现有任务置于前台并在该任务中启动 SampleActivity3。您的任务现在包含 SampleActivity2->SampleActivity3。您现在再次按 HOME。任务移至后台。

如果您现在使用第一个快捷方式,Android 会识别出您正在尝试启动现有任务的 "root" Activity,并且只会将现有任务置于前台并且不会向其中启动任何新的 Activity。该任务包含 SampleActivity2->SampleActivity3,因此您将看到 SampleActivity3,因为这是任务中的顶部 Activity。此行为与您使用 Whatsapp 运行 时的行为完全相同,请按主屏幕,然后按主屏幕上的 Whatsapp 图标。这不是 "start Whatsapp from the beginning",它只是将包含 Whatsapp 的当前任务带到前台,无论它在移动到后台时处于什么状态。

如果您现在按 HOME 键并使用第二个快捷方式,Android 会找到与 SampleActivity3 具有相同 taskAffinity 的任务,并将现有任务置于前台。 Android 现在要么什么都不做(如果 SampleActivity3launchMode="singleTop"),要么创建一个新的 SampleActivity3 实例并将其启动到任务中。在这种情况下,您的任务将包含 3 个 Activity 实例:SampleActivity2->SampleActivity3->SampleActivity3 您将看到 SampleActivity3,因为这是顶部 Activity 在任务中。


注意: 从下面的评论讨论中可以看出,重要的是添加

addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

到用于启动 ActivityIntent 如果你想确保创建 Activity 的新实例。否则,Android 只会将现有任务带到前台,不会启动任何新的 Activity