jhorvat
jhorvat

Reputation: 385

Clearing Activity Stack on Intent Start

I have an app that I'm writing that starts with an initial Login/Create New Account splash screen. In onCreate it checks my SharedPreferences to see if user credentials are stored. If they are the activity launches an intent to the main activity skipping the login/create process.

if (haveCredentials()) {
    Intent i = new Intent(this, MainActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
}

I don't want the user to be able to go back to the splash screen. As you can see I'm trying the CLEAR_TOP Intent flag as suggested by this but it doesn't seem to be working. Not sure what I'm missing.

Current State of the Activity Stack

SplashActivity->MainActivity

Desired State of the Stack

MainActivity

Upvotes: 0

Views: 83

Answers (2)

Manu
Manu

Reputation: 608

Have you tried finishing the current activity?

if (haveCredentials()) {
    Intent i = new Intent(this, MainActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
    finish(); //add this!
}

Upvotes: 1

Oam
Oam

Reputation: 922

Just use finish() after startActivity(), so that when back button is pressed the previous activity will not be shown.

Upvotes: 0

Related Questions