Search code examples
androidback

How to clear the rest of the backstack from login to an activity which is not root?


How to clear the rest of the backstack from login to an activity which is not root?


Solution

  • Found the solution:

    For Login back to Page 1

    • add the flag Intent.FLAG_ACTIVITY_CLEAR_TOP so that everything above Page 1 gets removed from the back stack
    • then I needed to call finish() at Login so that it gets removed from the back stack

    I do have other concerns but was able to resolve it, I'm adding it here just in case.

    1. Going back to home, I added the flag Intent.FLAG_ACTIVITY_CLEAR_TASK
    2. Going back to fragment and re-opening the selected tab, I used shared preference to record which tab to open, then at the activity where the fragment is I used onResume to check the saved shared preference to set the right tab.
    3. For opening a new intent which is not included in the back stack, I called the intent which should come before that new intent with an extra, I used that extra to open the new intent. Not sure if this is the right way to do it but it works.