我有一个应用程序,允许您为特定的 Activity
创建主页“快捷方式”。事实证明,我的一些用户会使用该应用程序,按下主页键去做其他事情,然后使用其中一个快捷方式跳回到那个 Activity。由于该应用程序仍在内存中,它只是在其他 Activity 之上打开新的 Activity
,然后“返回”键将它们带回整个历史记录。我想要发生的是,如果他们使用快捷方式,则可以有效地删除历史记录并让后退键退出应用程序。有什么建议吗?
最佳答案
首先,设置 taskAffinity在 list 中使 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);
关于android - 你如何让 Android "Home"快捷方式绕过它指向的应用程序的历史记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/978172/