Search code examples
androidandroid-activityback-stack

How to remove activity from activity backstack?


I have two activities Activity A and Activity B. When I click button in Activity A, The Activity B starts. Now When I press back button from Activity B the Activity A get restarted. But I want to come out of the app when the back button in Activity B is pressed. I tried using this but not getting success

Intent intent=new Intent(ActivityA.this, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Thanks in advance


Solution

  • You need to finish ActivityA once you're starting ActivityB:

    Intent intent=new Intent(ActivityA.this, ActivityB.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    finish();