Android:从返回堆栈中删除所有先前的活动

2023-11-26

当我点击Logout按钮在我的Profile我想带用户去的活动Login页面,他需要在其中使用新的凭据。

因此我使用了这段代码:

Intent intent = new Intent(ProfileActivity.this,
        LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

in the onButtonClick的注销按钮。

但问题是,当我单击登录活动上的设备后退按钮时,它会将我带到 ProfileActivity。我期望当我按下 LoginActivity 上的设备后退按钮时应用程序应该关闭。

我究竟做错了什么?

我还添加了android:launchMode="singleTop"在我的清单中登录活动

谢谢


提出的解决方案here为我工作:

Java

Intent i = new Intent(OldActivity.this, NewActivity.class);
// set the new task and clear flags
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);

Kotlin

val i = Intent(this, NewActivity::class.java)
// set the new task and clear flags
i.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(i)

但是,它需要 API 级别 >= 11。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:从返回堆栈中删除所有先前的活动 的相关文章

随机推荐