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

public void gotoC()
{
  Intent intent = new Intent(B.this, C.class);
    startActivityForResult(intent, 10);
}

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 10:


            }
        }
    }

@Override
    public void onBackPressed() {

            super.onBackPressed();

    }

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

 public void gotoD()
{
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

public void gotoB()
{
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"