Clear Android Activity Stack Starting New Activity

I have an application and each new action created will run an asynchronous task to validate the user session. If the session is valid, application flows continue. If not, the entire action stack should be cleared and only the login activity should be. This action has a no history flag, so it is never stored on the stack.

I have tried some of the solutions suggested here: Android: clear the activity stack but with no success.

This should work on the lowest android being at least 2.2

Thank!

+3


source to share


3 answers


I keep my activity on the stack. In the onResume () of the login activity, I check if the user has user credentials, and if so, call startActivity on the next screen presented after login. In this case, the user does not see the login screen.

When the user hits the logout button, I clear the user's credentials and then clears the stack all the way to the login screen:

    Intent intentLaunchLogin = new Intent(this, ActivityLogin.class);
    intentLaunchLogin.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intentLaunchLogin);

      



Also, if the user is on the screen presented after logging in and they hit the back button, I don't want them to go to the login activity. This code will send the user to the home screen as you would expect:

moveTaskToBack(true);

      

+3


source


Could you please do something like described here:

http://blog.janjonas.net/2010-12-20/android-development-restart-application-programmatically



basically you create an alarm that starts your intent and you close the application completely.

0


source


This is what I always do and work great. I am running an app with a main activity, checking if the user is logged in, if not logged in to start a login activity like this

void launchLoginActivity(){
 /* Move user to LoginActivity, and remove the backstack */
    Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
    finish();
}

      

It won't let u go back

0


source







All Articles