简体   繁体   English

在意图启动时清除活动堆栈

[英]Clearing Activity Stack on Intent Start

I have an app that I'm writing that starts with an initial Login/Create New Account splash screen. 我有一个正在编写的应用程序,以初始“登录/创建新帐户”初始屏幕开始。 In onCreate it checks my SharedPreferences to see if user credentials are stored. 在onCreate中,它检查我的SharedPreferences以查看是否存储了用户凭据。 If they are the activity launches an intent to the main activity skipping the login/create process. 如果是活动,则该活动将启动主要活动,从而跳过登录/创建过程。

if (haveCredentials()) {
    Intent i = new Intent(this, MainActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
}

I don't want the user to be able to go back to the splash screen. 我不希望用户能够返回到初始屏幕。 As you can see I'm trying the CLEAR_TOP Intent flag as suggested by this but it doesn't seem to be working. 正如你可以看到我想要的CLEAR_TOP意图标志所建议这个 ,但它似乎并不奏效。 Not sure what I'm missing. 不知道我在想什么。

Current State of the Activity Stack 活动堆栈的当前状态

SplashActivity->MainActivity

Desired State of the Stack 所需的堆栈状态

MainActivity

Have you tried finishing the current activity? 您是否尝试过完成当前活动?

if (haveCredentials()) {
    Intent i = new Intent(this, MainActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    finish(); //add this!
}

只需在startActivity() finish()之后使用finish() ,这样在按下后退按钮时将不会显示前一个活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM