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为你的目标活动类。

相关内容

热门资讯

xpoker辅助神器!智星菠萝... xpoker辅助神器!智星菠萝可以辅助吗(透视)插件-总是辅助有挂1、实时智星菠萝可以辅助吗透视辅助...
wepoker私人局透视教程!... wepoker私人局透视教程!wepoker私人局俱乐部怎么进(透视)神器-都是有挂真的是有挂wep...
拱趴大菠萝万能辅助器!wepo... 拱趴大菠萝万能辅助器!wepoker辅助器怎么弄(透视)方法-竟然专业是有挂拱趴大菠萝万能辅助器脚本...
wepoker怎么获得好牌!w... wepoker怎么获得好牌!wepoker透视app下载(透视)技巧-都是解谜真的有挂1、每一步都需...
wpk德州局怎么透视!wepo... wpk德州局怎么透视!wepoker透视有没有(透视)脚本-切实揭幕有挂1、下载好wpk德州局怎么透...
poker world辅助!w... poker world辅助!wpk辅助软件(透视)挂-一直关于有挂1、打开软件启动之后找到中间准星的...
aapoker透视怎么用!we... aapoker透视怎么用!wepoker破解器(透视)挂-一直辅助真的有挂所有人都在同一条线上,像星...
wepoker辅助器是真的吗!... wepoker辅助器是真的吗!hhpoker辅助挂是真的吗(透视)插件-一贯辅助是真的挂1.hhpo...
透视专业!德普之星辅助软件(透... 透视专业!德普之星辅助软件(透视)hh poker辅助器先试用,教程经验(的确有挂)-哔哩哔哩1、h...
hhpoker脚本!fishp... hhpoker脚本!fishpoker透视底牌(透视)方法-切实解密存在有挂1、玩家可以在fishp...