Search code examples
androidandroid-activity

How to clear current activities in the stack?


Support App starts activity A, then A starts activity B and finishes itself. After that activity B starts activity C.

Now the stack contains B and C, with C at the top.

Then I click a button in activity C, and want it to clear B and C and start activity A, i.e. I want activity A to be the only activity in the stack. How can I make it?

Edit: I made a test to use FLAG_ACTIVITY_CLEAR_TOP. But it didn't work in my case, because activity A is not running when button in activity C is clicked.


Solution

  • Set the FLAG_ACTIVITY_CLEAR_TOP flag on your intent to start activity A.

    Edit: Is there a reason you can't leave A going? Then you could do as suggested.

    Otherwise, another (more complicated) option:

    In B start C forResult. When A is started from C, you could finish C with a result indicating to B to also exit.