【问题标题】:Android dynamic shortcuts not opening proper activity second timeAndroid动态快捷方式第二次没有打开正确的活动
【发布时间】:2017-09-07 12:37:29
【问题描述】:

您好,我正在为 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,这是最后一个最小化的活动。 如果我用静态的快捷方式做同样的事情,那么它工作正常:

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

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

【问题讨论】:

  • 发布您的清单。
  • 使用清单代码更新问题。请帮帮我。
  • 查看我的回答,了解正在发生的事情。你希望发生什么?

标签: android android-activity shortcuts android-static-shortcuts android-dynamic-shortcuts


【解决方案1】:

这可以在您发布清单后得到确认,但我猜正在发生以下情况:

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

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

如果您现在使用第一个快捷方式,Android 会识别出您正在尝试启动现有任务的“根”Activity,并且只是将现有任务置于前台而不启动任何新的Activity它。该任务包含SampleActivity2->SampleActivity3,因此您将看到SampleActivity3,因为这是任务中的顶部Activity。此行为与您在运行 Whatsapp 时完全相同,例如,按 HOME,然后按 HOME 屏幕上的 Whatsapp 图标。这不会“从头开始启动 Whatsapp”,它只是将包含 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);

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

【讨论】:

  • 在更多的观察中我发现了同样的事情。如果我使用具有相同意图的静态快捷方式来实现它。然后它工作正常。但是如果我用动态的快捷方式来做,它就不能正常工作。这两种方法有什么区别。如果不是,那么它在两种情况下都应该以类似的方式表现。如果有任何错误,请纠正我。我也用静态方式更新我的问题。请帮帮我。
  • 请解释“它工作正常”是什么意思。如果您的应用程序在后台从快捷方式 1 启动并且您单击快捷方式 2,您期望会发生什么?您是否测试过当您的应用程序运行并使用静态快捷方式并按下 BACK 按钮时会发生什么?大约有十几种不同的方法来构建它,这取决于您在每种情况下想要(或认为您想要)的导航。您可以随时使用adb shell dumpsys activity activities 查看活动堆栈
  • 所以静态快捷方式的情况是这样的——>点击快捷方式1打开SampleActivity1。单击主页按钮并将应用程序带到后台。现在单击快捷方式2,它会打开 SampleActivity2。单击主页按钮并将应用程序带到后台。现在再次单击快捷方式 1 -> 它会打开 SampleActivity1,这很好。
  • 所以 Android 在启动快捷方式之前正在清除任务堆栈。您可以通过调用addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK) 来模拟您自己用于动态快捷方式的快捷方式Intents。请尝试一下。
  • 是的,使用这两个标志我能够实现我的结果。谢谢你的帮助。
猜你喜欢
  • 2017-08-09
  • 2015-05-02
  • 1970-01-01
  • 2011-10-23
  • 2021-04-19
  • 2015-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多