4

I have an application and every new created activity will start an async task to validate the user session. If the session is valid, the application flows continues. If not, the whole activity stack must be cleared and there should be only the login activity. This activity has a "no history" flag so it is never kept in the stack.

I've been trying some solutions provided here: Android: Clear Activity Stack but with no success.

This must works on the lowest android possible, being the least 2.2

Thanks!

braX
  • 11,506
  • 5
  • 20
  • 33
Miguel Ribeiro
  • 8,057
  • 20
  • 51
  • 74

3 Answers3

3

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

When the user presses the logout button, I clear the user's credentials and then this clears the stack all the way back 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 the login and they press 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 would be expected:

moveTaskToBack(true);
Andrew
  • 20,756
  • 32
  • 99
  • 177
  • I'll give it try ;) just a doubt, the moveTaskToBack function must be placed on all activities except for the login one or in the login one? – Miguel Ribeiro Apr 06 '12 at 20:12
  • It worked, however, a did a slight change to the activity after login. I hijacked the back button action to moveTaskToBack and finish activity. I want it to start from the Login activity every time the app resumes – Miguel Ribeiro Apr 06 '12 at 20:34
  • 1
    I only have the moveTaskToBack call in the onBackPressed handler on the Activity after login because I don't want users to see the login screen again. I only want the login screen to appear if the user is logged out. – Andrew Apr 06 '12 at 20:41
0

Could you do something like is described here:

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

basically you create an alarm that starts your intent, then you close your app completely.

John Boker
  • 82,559
  • 17
  • 97
  • 130
0

This is what I always do and works perfectly. I start the app with the main activity an check if the user is logged in, if he is not logged in launch the 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 will not allow u to go back

Miguel Carvajal
  • 1,785
  • 19
  • 23