user1910290
user1910290

Reputation: 537

How to destroy previous activity

So I have 2 activities lets say A and B. A navigates to B, I want the activity A to be killed or make it unusable/unseen when directed from B. so it should be like when I press the back button on B activity it should not open activity A instead it should go to the app tray.

also activity A should comeback when I clear the application data thanks.

Upvotes: 1

Views: 286

Answers (1)

Michael Celey
Michael Celey

Reputation: 12745

You could do this in one of two ways. First is finishing ActivityA so that it can't be resumed to later. When starting ActivityB from ActivityA, you'd do something like this:

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

Another way is to just finish ActivityA when it gets a result of any sort from ActivityB. This code would also be in ActivityA.

To start ActivityB:

Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, REQUEST_ACTIVITYB);

To make sure ActivityA doesn't resume:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == REQUEST_ACTIVITYB) {
        finish();
    }
}

REQUEST_ACTIVITYB is just an int of your choosing.

Upvotes: 2

Related Questions