简体   繁体   中英

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

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.

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).

User can go back and forward between MainActivity and SigInActivity. How can I prevent users going back to SignInActivity or MainActivity from FirstPage?

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. 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. Try to use both flags (NEW_TASK and CLEAR_TOP).

You also could override your onBackPressed method.

@Override
public void onBackPressed() {
      return; 
}

This post has solved a very similar problem, worth to look at it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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