Ján Srniček
Ján Srniček

Reputation: 525

How to start activity and end all other activities that was launched before

I have one login activity in my soft and one main activity . What i want to do is start login activity ,log user and then start the main activity ,and delete the login activity from the stack so the user can acess the login activity by pressing back button. Whitch intend flag should i use?

Upvotes: 3

Views: 2320

Answers (2)

Sash_KP
Sash_KP

Reputation: 5591

Okay i also dealt with this issue lately.you need to override onBackPressed() method in order to restrict the operations on pressing the back button.

so what you should do is,in the main activity write,

public void onBackPressed() {

new AlertDialog.Builder(this)
        .setTitle("Alert")
        .setMessage("Please Log out first.")
        .setpositiveButton("Ok", null)
            .create().show();
}

This way user can't go back to the login page without logging out in the mainactivity page.

Also read about the use of finish().

This might help.Check it out.

Upvotes: 1

Michael Yaworski
Michael Yaworski

Reputation: 13483

Intent intent = new Intent(currentClassYoureIn.this, newClassYouWantToBeIn.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // closes all activities that were started after "newClassYouWantToBeIn"
startActivity(intent);

That, or you could just start your new intent and then use the method finish() to close the current activity (that you're starting the new intent in):

Intent intent = new Intent(currentClassYoureIn.this, newClassYouWantToBeIn.class);
startActivity(intent);
finish(); // closes "currentClassYoureIn" and now "newClassYouWantToBeIn" is the only activity up

Upvotes: 2

Related Questions