Android Stack Management

I have an activity hierarchy, and I have a button that allows me to go from action D to activity B.

image

The thing is, switching to B from D would leave C on the back side, so if I do A-> C-> D-> B and then bring it back, it will send me to C, not to A (which this is what i want).

Is there a way to remove C when I press a button from B, or is there some workaround?

+4
source share
2 answers

Consider using Aas a dispatcher. When you want to start Bfrom Dand complete Cin this process, do this in D:

// Launch A (our dispatcher)
Intent intent = new Intent(this, A.class);
// Setting CLEAR_TOP ensures that all other activities on top of A will be finished
//  and setting SINGLE_TOP ensures that a new instance of A will not
//  be created (the existing instance will be reused and onNewIntent() will be called)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
// Add an extra telling A that it should launch B
intent.putExtra("startB", true);
startActivity(intent);

A.onNewIntent() :

@Override
protected void onNewIntent(Intent intent) {
    if (intent.hasExtra("startB")) {
        // Need to start B from here
        startActivity(new Intent(this, B.class));
    }
}
+1

, B, C D , C, D, .

C, D, :

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

C D.

, , .

0

Source: https://habr.com/ru/post/1617132/


All Articles