0

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!

Jerry Gor
  • 13
  • 3

2 Answers2

1

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.

Bryan Dormaier
  • 800
  • 6
  • 10
1

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!

  • Thanks, but still not working perfectly. SignInPage is dimissed, but I head back to the very first activity when i clicked the back button. – Jerry Gor Feb 05 '18 at 23:42