Android Studio - 如何在使用 flag_activity_clear_task | flag_activity_new_task 后修复返回按钮仍然可用?
创始人
2024-08-16 01:30:18
0

要在使用FLAG_ACTIVITY_CLEAR_TASKFLAG_ACTIVITY_NEW_TASK后修复返回按钮仍然可用,可以通过在启动新任务时保存任务栈的根引用,并在需要时恢复根引用来实现。

以下是一个示例代码,演示如何在启动新任务后修复返回按钮可用:

// 在Activity A 中启动新任务
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

// 在启动新任务之前保存根引用
SharedPreferences preferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("rootActivity", getLocalClassName());
editor.apply();

接下来,在需要修复返回按钮可用的地方,可以使用以下代码获取根引用并启动它:

SharedPreferences preferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
String rootActivity = preferences.getString("rootActivity", null);

if (rootActivity != null) {
    try {
        Intent intent = new Intent(this, Class.forName(rootActivity));
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

通过这种方式,用户将能够在新任务中使用返回按钮,而不会返回到旧任务的活动中。

请注意,上述示例代码假设你已经正确设置了MainActivity作为新任务的启动活动。根据你的应用程序的具体情况,你可能需要相应地更改MainActivity为你的目标活动类。

相关内容

热门资讯

透视好友房!we-poker辅... 透视好友房!we-poker辅助,wepoker怎么设置盖牌(详细钻石可以看底牌)1、起透看视 we...
透视软件!aapoker辅助工... 透视软件!aapoker辅助工具安全吗(透视)aapoker免费透视脚本,AI教程(有挂解说);aa...
透视最新!wepoker免费脚... 透视最新!wepoker免费脚本,wepoker怎么设置房间(详细有透视功能);1、玩家可以在wep...
透视挂!aapoker辅助怎么... 透视挂!aapoker辅助怎么用(透视)aapoker辅助插件工具,解密教程(有挂解说)1、点击下载...
透视了解!wepoker养号规... 透视了解!wepoker养号规律,wepoker透视是真的吗(详细软件);1、下载好wepoker透...
透视教学!aapoker怎么提... 透视教学!aapoker怎么提高中牌率(透视)aapoker俱乐部靠谱吗,揭秘攻略(有挂细节);一、...
透视科技!wepoker永久免... 透视科技!wepoker永久免费脚本,wepoker透视脚本免费下载pc(详细轻量版透视);1、该软...
透视肯定!aapoker公共底... 透视肯定!aapoker公共底牌(透视)aa poker辅助,详细教程(有挂教程)1、很好的工具软件...
透视黑科技!wepokerpl... 透视黑科技!wepokerplus到底是挂了吗,wepoker作弊视频(详细免费脚本咨询)1、下载好...
透视免费!aapoker脚本怎... 透视免费!aapoker脚本怎么用(透视)aapoker脚本怎么用,专业教程(有挂攻略);1、用户打...