android - 你如何让 Android "Home"快捷方式绕过它指向的应用程序的历史记录?

标签 android android-activity shortcut

我有一个应用程序,允许您为特定的 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_TASKFLAG_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/

相关文章:

java - 将 JSON 字符串分隔为 android 中的变量

android - 从服务更新 UI

android - 如何在 Android 中使用 "Send Feeback"FeedbackActivity?

android - 在 Android 中退出应用程序

c++ - 寻找一个函数(或宏)来返回一个 boost::scoped_lock

java - Android 主屏幕快捷方式权限错误

android - 如何让Android Pie中的后台服务继续运行?

android - 如何防止所有 Activity 屏幕在android中旋转

java - 无需 setRequestedOrientation 即可更改方向

c# - 如何删除整个项目中未使用的 using 语句?