[英]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
,這是最后一個最小化的活動。 如果我用靜態的快捷方式做同樣的事情那么它工作正常:
<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會為您的應用創建一個新任務,並將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會使用與SampleActivity3
相同的taskAffinity
找到您的任務,並將現有任務帶到前台。 Android現在不執行任何操作(如果SampleActivity3
具有launchMode="singleTop"
)或創建SampleActivity3
的新實例並將其啟動到任務中。 在這種情況下,您的任務將包含3個Activity
實例: SampleActivity2
- > SampleActivity3
- > SampleActivity3
,您將看到SampleActivity3
因為這是任務中的頂級Activity
。
注意:從下面的評論討論中可以看出,重要的是要添加
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
如果要確保創建Activity
的新實例,則使用用於啟動Activity
的Intent
。 否則,Android將把現有任務帶到前台,不會啟動任何新的Activity
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.