Search code examples
androidandroid-activity

Removing an activity from the stack


When going from activity A to B, I want to clear A off the stack: so when the user is pressing the back button in activity B, the app exits.

Intent intent = new Intent(A.this, B.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

These code lines do not work - the app goes back to activity A. I've also tried to OR with the flag Intent.FLAG_ACTIVITY_NEW_TASK, same result. I've actually also tried FLAG_ACTIVITY_NO_HISTORY.

I'm using Android 2.2 for my app.


Solution

  • Just call finish() after you call startActivity(). It should clear Activity A from the stack. In code it looks like this:

    Intent intent = new Intent(A.this, B.class);
    startActivity(intent);
    finish();