Clear Activity Stack and start new activity in android

It's bit old question, but maybe someone else will stumble upon it while searching answer to similar problem.

You should start Login activity with flags: Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK - flag NEW_TASK may have a little bit confusing name, but it will actually create new task just if it doesn't exist (otherwise current task will be used) - and CLEAR_TASK will clear it from all previous activities.


Try this,

Finish your current Activity

     YourCurrentActivity.this.finish();  
     Intent intent1 = new Intent(YourCurrentActivity.this,LoginActivity.class);  
     intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
     startActivity(intent1); 

It will work even your activity is not in the stack.

Hope it helps.


Use this

Intent i = new Intent(yourScreen.this,Home.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.putExtra("EXIT", true);
        startActivity(i);

and in the onCreate of the Home class, do this to check,

if (getIntent().getBooleanExtra("EXIT", false)) 
    {
        Intent i = new Intent(Home.this,Login.class);
        startActivity(i);
        finish();
    }

what this will essentially do is no matter at what activity you are, you can call the home screen with the clear top flag. In the home screen there is a check condition in the onCreate method which will help to clear the stack and take you to the login screen.. Now on the login screen,if you press back button you will exit the app as the stack is cleared..

Let me know if the problem still persists...