简体   繁体   English

Android禁止返回以前的活动

[英]Android prevent going back to previous activities

I am developing on an Android app in which I want to prevent going back to SignIn activity after user logged in. I have tried Intent.FLAG_ACTIVITY_CLEAR_TASK, but it doesn't work 我正在开发一个Android应用程序,在该应用程序中我想防止用户登录后返回登录活动。我已经尝试过Intent.FLAG_ACTIVITY_CLEAR_TASK,但是它不起作用

Intent intent = new Intent(SignInActivity.this, FirstPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

And I have also tried adding finish right after start Activity, but that simply gives me a leaked window error. 而且我还尝试在启动Activity之后立即添加完成,但这只是给我一个泄漏的窗口错误。

Intent intent = new Intent(SignInActivity.this, FirstPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

The path of my activities is: 我的活动路径是:

MainActivity(description of my app) <-> SignInActivity(sign in) -> First(logged in). MainActivity(我的应用程序说明)<-> SignInActivity(登录)-> First(已登录)。

User can go back and forward between MainActivity and SigInActivity. 用户可以在MainActivity和SigInActivity之间来回移动。 How can I prevent users going back to SignInActivity or MainActivity from FirstPage? 如何防止用户从FirstPage返回SignInActivity或MainActivity?

Any idea? 任何想法? Thank you! 谢谢!

Calling finish() will end the activity and not have it on the stack as an activity you can go back to. 调用finish()将结束该活动,并且不将该活动作为您可以返回的活动放在堆栈中。 You need to look into what is causing your leaked window error (such as leaving a notification on screen when you finish the activity) and resolve that rather than not using finish because you have a leaked window error. 您需要调查导致窗口泄漏错误的原因(例如,在完成活动时在屏幕上留下通知),并解决该问题,而不是因为您遇到窗口泄漏错误而不使用完成。

With the FLAG_ACTIVITY_NEW_TASK your activity will become the start of a new task of the history stack. 使用FLAG_ACTIVITY_NEW_TASK,您的活动将成为历史记录堆栈新任务的开始。 Try to use both flags (NEW_TASK and CLEAR_TOP). 尝试同时使用两个标志(NEW_TASK和CLEAR_TOP)。

You also could override your onBackPressed method. 您还可以覆盖onBackPressed方法。

@Override
public void onBackPressed() {
      return; 
}

This post has solved a very similar problem, worth to look at it! 这篇文章解决了一个非常相似的问题,值得一看!

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

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