In my app there are three activities.
A
can open B
B
can open C
, or return to A
, or return to C
(if opened from C
)
C
can open B
, or return to A
I have implemented this using various RESULT_CODE
s and onActivityResult
s. However, the stack becomes too large after prolonged use of the app!
I need to clear activities from the stack. Looking at my app's structure, the best clearing method revolves around clearing whatever is the second activity on the stack (assuming A
is always the first/bottom activity on the stack). I made a quick painting to make it easier to understand:
Once B
is added onto ABC
, the previous B
(second activity in the stack) is removed. Once C
is added onto ACB
, the previous C
(second activity in the stack) is removed.
I can think of two methods that do what I describe:
clear whatever is the second activity in the stack
remove any activities in the stack that are the same as the one that I am creating (
C
createsB
, so the otherB
needs to be cleared)
However, I have not found a way to implement this in code. I have tried using Intent.FLAG_ACTIVITY_CLEAR_TOP
, but that clears everything in the stack except for A
, before adding the new activity at the top.
So my question: How can I implement method 1 or method 2? (Does not matter which one)
Here is an example of me clearing all in the stack but A
:
Intent newActivity = new Intent(this, B.class);
newActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(newActivity, MY_REQUEST_CODE);
I am also open to alternative methods. Maybe using FLAG_ACTIVITY_NO_HISTORY
somehow?