3

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?

Ján Srniček
  • 505
  • 1
  • 10
  • 34
  • What do you mean, so the user can acess the login activity or it should be so the user can not access the login activity. – Md. Monsur Hossain Tonmoy Sep 22 '13 at 18:07
  • that he can not acess with back button only with the new intend of it that will stop the main activity in case of prevent errors and problems with reloging the user while he does not log out – Ján Srniček Sep 22 '13 at 18:44

2 Answers2

2
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
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • 1
    Intent.FLAG_ACTIVITY_CLEAR_TOP does not finish all activity. For example, you have 4 activities, A, B, C, and D, and the flow is A -> B -> C -> D and now when you are on D you want to start activity B (from the stack and not a new instance) then you can use this intent flag. Also what it does is remove all the other activities on top of B (here C and D not A). So new status will be A -> B . for more info see this http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP – Md. Monsur Hossain Tonmoy Sep 22 '13 at 18:27
  • 1
    If you are suggesting something like this Intent intent = new Intent(Login.this, main.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); believe me i will not finish login activity. Please check – Md. Monsur Hossain Tonmoy Sep 22 '13 at 18:50
  • If he just want to finish login activity he can simply start main activity and then call the finish function. Like Intent intent = new Intent(LoginActivity.this, mainActivity.class); startActivity(intent); finish(); – Md. Monsur Hossain Tonmoy Sep 22 '13 at 18:55
  • @mikeyaworski No, this does not close actvities that are on the same stack but below the launched activity. Md. Monsur Hossain Tonmoy is absolutely right! – IgorGanapolsky Jun 25 '15 at 15:03
1

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.

Community
  • 1
  • 1
Sash_KP
  • 5,551
  • 2
  • 25
  • 34