0

I have a login screen on my app, and as of now you can press the back button to go back to the login screen. I do not want that to happen.

Once the user logs in, the app should stop the login activity so the user can't go back to it. On the menu screen I have a logout button that I want to use to restart the login activity after already being stopped.

I have been looking and testing for an hour or so and I can't find the answer.

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
user1513687
  • 173
  • 2
  • 13

5 Answers5

1

Set noHistory = true in the manifest for your login activity. This prevents the activity from being stored on the back stack.

kabuko
  • 36,028
  • 10
  • 80
  • 93
0

On successful login you can call Actvity.finish().

To restart the login activity use Activity.startActivity(intent).

techi.services
  • 8,473
  • 4
  • 39
  • 42
0

Try either:

After login succeeds in your login activity, call finish() on the activity after launching the next activity. This will effectively destroy the login activity and remove it from the activity back stack (so it can't be navigated to anymore). Something like:

onLoginComplete(){
    startActivity(new Intent(this, NextActivity.class));
    finish();
}

Or, in the onResume of your login activity check the login status and finish if true. This way, whenever the user comes to this activity it will be closed (and they will never actually see it) if they are already logged in. Something like:

@Override
public void onResume(){
    super.onResume();
    if(isLoggedIn())
        finish();
}
newbyca
  • 1,523
  • 2
  • 13
  • 25
0

When you leave the login screen on a successfull login, call finish() this will remove your actvity from the stack.

Your logout button should re-call the login page like this:

Intent intent = new Intent(TheCurrentActivity.this, YourLoginScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

This will load up the login screen and clear the current activity stack ensuring that you can't go back after logout.

melodiouscode
  • 2,105
  • 1
  • 20
  • 41
0

If you do not want user to be able to get back to login activity, simply exclude it from recent stack, by adding to your activity declaration in application manifest android:excludeFromRecents attribure, like this:

<activity android:name=".Login" android:excludeFromRecents="true"></activity>

then once your login activity finishes it task, you (most likely) go to next activity of your app. And once you call startActivity() to lauch new one, simply call finish(); to finish login actvity:

...
startActivity( intentToLaunchNextActivity );
finish();
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141