0

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.

gosr
  • 4,593
  • 9
  • 46
  • 82

3 Answers3

1

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();
onit
  • 6,306
  • 3
  • 24
  • 31
0

Two solutions: in your B activity:

@Override
public void onBackPressed() {
    super.onBackPressed(); //not sure if this line is needed
    System.exit(0);
}

Or a much nicer: Start your activity with startActivityForResult, and implement your A activity's onActivityResult method:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
   finish();
}
0

FLAG_ACTIVITY_NO_HISTORY

If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.

So, You can implement this from your

AndroidManifest.xml file,

android:noHistory="true"

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Mehul
  • 573
  • 1
  • 16
  • 33