Skip to content Skip to sidebar Skip to footer

Android Clear Some Activity On Back Stack

Let Say I have A -> B -> C -> D or A -> C-> D when D is finish I want to Back but skip C, it will back to A or B. But When User use back button it will back normall

Solution 1:

Write this code in B Activity

publicvoidgotoC()
{
  Intentintent=newIntent(B.this, C.class);
    startActivityForResult(intent, 10);
}

 @OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case10:


            }
        }
    }

@OverridepublicvoidonBackPressed() {

            super.onBackPressed();

    }

ForwardActivityResult in C Class when you call Intent to go D Activity

publicvoidgotoD()
{
Intent intent = new Intent(Activity.C, D.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
      startActivity(intent);
      finish();
}

In D Activity GO to B activity Call RESULT_OK

publicvoidgotoB()
{
Intent intent = new Intent();
     setResult(RESULT_OK, intent);
     finish();
}

Solution 2:

You can use

android:noHistory="true"

in manifest while declaring activity for activity B

Solution 3:

I think you can use this code before open D finish()

Post a Comment for "Android Clear Some Activity On Back Stack"