Search code examples
androidandroid-intentback-stack

Treat inherited Activities as the same for Reorder to Front


I have a project with 3 Activites A, B and C. Activity B extends A.

A and B should be single instance, so I add Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT to my intents when switching activities. This works correctly in that navigating A > B > A > C > B gives me the back stack A > C > B only.

But ideally I want A and B to be treated the same in the context of the backstack as they appear the same to the user, they just have differences under the cover.

This isn't my own project so I don't have the flexibility to change the Activity structure. Is there a way to make A and B be treated as the same? I tried taskAffinity but I don't want the separate entry on recents nor the visible switch/animation to a new task. I also considered CLEAR_TOP, but it is important not to remove other activies from the stack (ie C)

What I expect is that navigating A > B > A > C > B should give me the back stack C > B only.


Solution

  • No, there is no way to have Android treat these 2 Activities the same in terms of the back stack.

    You should move the code from ActivityB to ActivityA and use an "extra" in the Intent to indicate whether the Activity should behave like A or like B. This is the only way to be able to ensure that either A or B will be in the stack, but not both.