Search code examples
androidandroid-intentandroid-activityactivity-stack

Clear activity stack in android


I have a LoginActivity as my launcher screen. And SettingsActivity as my fifth screen.

I have a logout button in settings screen. On clcik of this button, how can i go to Screen-1 (i.e LoginActivity) by clearing all remaining activities (i.e 2nd, 3rd, 4th) from activity stack?

Note : i have finished LoginActivity but not remaining activities. Thanks in advance


Solution

  • Simple, set an onclick on your logout button and then add this to your intent:

    Intent newIntent = new Intent(this, login.class);
    
        newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
        startActivity(newIntent);
        finish();
    

    This should clear your entire stack above login.class

    And if you're feeling real fun today, add

        newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    

    Which will get rid of that annoying "show the android homescreen" issue on the S3