gosr
gosr

Reputation: 4708

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.

Upvotes: 0

Views: 180

Answers (3)

Mehul
Mehul

Reputation: 575

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"

Upvotes: 0

onit
onit

Reputation: 6366

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();

Upvotes: 1

Antal Horváth
Antal Horváth

Reputation: 151

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();
}

Upvotes: 0

Related Questions