nil*_*ash 5 shortcuts android android-activity android-static-shortcuts android-dynamic-shortcuts
嗨,我正在开发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);
}
Run Code Online (Sandbox Code Playgroud)
我的清单看起来像这样:
<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>
Run Code Online (Sandbox Code Playgroud)
这段代码的问题是这样的.我从后台杀了应用程序.单击第一个快捷方式.它打开了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>
Run Code Online (Sandbox Code Playgroud)
在清单中我补充说:
<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>
Run Code Online (Sandbox Code Playgroud)
我的代码有什么问题吗?需要一些帮助.谢谢.
这可以在您发布清单后确认,但我猜正在发生以下情况:
当您使用第一个快捷方式时,Android 会为您的应用创建一个新任务并启动SampleActivity2
该任务。然后按 HOME,它只会将此任务发送到后台。
现在您使用第二个快捷方式。由于SampleActivity3
具有相同taskAffinity
的SampleActivity2
,Android将包含现有任务SampleActivity2
的前景和发射SampleActivity3
到该任务。您的任务现在包含SampleActivity2
-> SampleActivity3
。您现在再次按 HOME。任务移到后台。
如果您现在使用第一个快捷方式,Android 会识别出您正在尝试启动Activity
现有任务的“根” ,并且只会将现有任务置于前台,而不会向其中启动任何新任务Activity
。任务包含SampleActivity2
->SampleActivity3
所以你会看到SampleActivity3
因为这是Activity
任务的顶部。此行为与例如运行 Whatsapp,按 HOME,然后按 HOME 屏幕上的 Whatsapp 图标时完全相同。这不是“从头启动 Whatsapp”,它只是将包含 Whatsapp 的当前任务以它移到后台时所处的任何状态带到前台。
如果你现在按HOME并使用第二个快捷键,Android的使用相同的发现你的任务taskAffinity
是SampleActivity3
和带来的现有任务的前景。Android 现在要么什么都不做(如果SampleActivity3
有launchMode="singleTop"
),要么创建一个新实例SampleActivity3
并将其启动到任务中。在这种情况下,您的任务将包含 3 个Activity
实例:SampleActivity2
-> SampleActivity3
->SampleActivity3
并且您将看到,SampleActivity3
因为这是Activity
任务的顶部。
注意:从下面的评论讨论中可以看出,添加
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Run Code Online (Sandbox Code Playgroud)
如果要确保创建的新实例,则Intent
用于启动。否则,Android 只会将现有任务带到前台,而不会启动任何新的.Activity
Activity
Activity
归档时间: |
|
查看次数: |
866 次 |
最近记录: |