简体   繁体   English

在开始新活动之前清除android堆栈

[英]Clearing android stack before starting a new activity

I have an application with three activities: Login, Register and Main activity. 我有一个包含三个活动的应用程序:登录,注册和主要活动。

I want to start the Main activity after a user had logged in or registered. 我想在用户登录或注册后启动Main活动。 But, when the user presses the 'back' button, he should not see the activity from which he logged in. In other words, I want to clear the activity stack before starting the Main activity. 但是,当用户按下“后退”按钮时,他应该看不到登录的活动。换句话说,我想在开始Main活动之前清除活动堆栈。

I saw a few solutions online, for example: this , this and this . 我在网上看到了一些解决方案,例如: thisthisthis They did not help. 他们没有帮助。

This is my code for launching the main activity from the Login activity: 这是我从Login活动启动主活动的代码:

Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
getApplicationContext().startActivity(intent);

The code in the register activity is quite similar. 寄存器活动中的代码非常相似。

I also tried to put finish(); 我也试图把finish(); after I started the activity, but it only closed the current activity. 在启动活动之后,但它仅关闭了当前活动。 Meaning, if I started in the login activity, moved to the register activity and logged in there (Login -> Register -> Main), When I will press the 'back' button I will be return to the Login activity. 意思是,如果我从登录活动开始,移到注册活动并登录到那里(登录->注册->主菜单),当我按下“返回”按钮时,我将返回到登录活动。

Thank you for your help! 谢谢您的帮助! Yuval. 尤瓦。

The easy way would be to use "noHistory" attribute in manifest. 最简单的方法是在清单中使用“ noHistory”属性。 You can set it for the login activity in your manifest file 您可以在清单文件中为登录活动设置它

<activity
    android:noHistory="true"
/>

You can do it like this: 您可以这样做:

If the user goes to the registration screen then just finish the login activity and when they click the back button on their device you can call the onBackPressed () method. 如果用户转到注册屏幕,则只需完成登录活动,然后在他们的设备上单击后退按钮时,就可以调用onBackPressed()方法。 In there you can finish the registration screen and start the login activity. 您可以在其中完成注册屏幕并开始登录活动。 If the user went to the registration screen and then registers, you also want to finish the register activity and open the main menu or something else. 如果用户转到注册屏幕然后注册,则您还想完成注册活动并打开主菜单或其他菜单。

Login: 登录:

Click on register? 点击注册? Start register activity and finish the login 开始注册活动并完成登录

startActivity (new Intent (Login.this, Register.class));
finish ();

Register: 寄存器:

Click on submit? 点击提交? Start main activity and finish register activity 开始主要活动并完成注册活动

startActivity (new Intent (Register.this, Main.class));
finish ();

Click om back button? 单击om后退按钮?

@Override
public void onBackPressed (){
startActivity (new Intent (Register.this, Login.class));
finish ();

} }

And you can also use in the androidmanifest: 而且您还可以在androidmanifest中使用:

android:noHistory="true"

In the activities you want to have no history (where the user can't go to if they click on the back button). 在活动中,您希望没有历史记录(如果用户单击后退按钮,则无法进入该历史记录)。 But I prefer to do it in code! 但是我更喜欢用代码来做!

Hope this helps you! 希望这对您有所帮助!

Try this: 尝试这个:

Intent intent = new Intent(this, SomeOtherClass.class);

// do not keep this intent in history
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

What you are doing is almost correct, just make a small change in the Intent. 您所做的几乎是正确的,只需对Intent进行一点更改即可。

In place of using, 代替使用

    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    getApplicationContext().startActivity(intent);

use: 采用:

    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    getApplicationContext().startActivity(intent);

It will clear the top, start a new task and clear all the previous task as well. 它将清除顶部,开始新任务并清除所有先前的任务。 Now, all that Android knows is that there exists a MainActivity in your stack. 现在,Android所知道的就是您的堆栈中存在一个MainActivity。 You can write: 你可以写:

    @Override
    public void onBackPressed() {
         finish();
    }

in MainActivity which will allow the user to exit the application and not go back to the LoginActivity. 在MainActivity中,这将允许用户退出应用程序而不返回到LoginActivity。

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

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