你如何让一个Android“主页”快捷绕过它的点历史上的应用程序?
-
13-09-2019 - |
题
我有一个应用程序,允许你创建首页“快捷方式”,以一个特定的Activity
。事实证明,我的一些用户使用的应用程序,打的Home键去做些别的事情,然后使用快捷键一个跳回到该活动。由于应用程序仍在内存中它只是打开的人,和“返回”键,顶部的新Activity
会带他们回来贯穿整个历史。如果他们使用的快捷方式,然后以有效杀灭历史,有返回键只是退出程序我想有发生的。任何建议?
解决方案
首先,设置在 taskAffinity 在舱单使Activity
运行作为一个不同的“任务”:
<activity
android:taskAffinity=""
android:name=".IncomingShortcutActivity">
<intent-filter>
<action android:name="com.example.App.Shortcut"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
然后,构建快捷时,设置FLAG_ACTIVITY_NEW_TASK
和FLAG_ACTIVITY_CLEAR_TOP
标志。是这样的:
// build the shortcut's intents
final Intent shortcutIntent = new Intent();
shortcutIntent.setComponent(new ComponentName(this.getPackageName(), ".IncomingShortcutActivity"));
shortcutIntent.putExtra(EXTRA_STOPID, Integer.toString(this.stop_id));
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, custom_title);
// Set the custom shortcut icon
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.bus_stop_icon));
// add the shortcut
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
其他提示
尝试增加Intent.FLAG_NEW_TASK
到的意图。
不隶属于 StackOverflow