Android : Need help arranging the Tasks and Back Stack

188 Views Asked by At

I'm sorry if this sounds stupid and trivial but I'm stuck on it for a long time.

I have 5 Activities A,B,C,D,E. And the chain goes like this A->B(finished after moving to C)->C->D->E. Since C,D are interdependent so I don't want to loose any instances of C,D until I finish both of them together. And I want to do this in E, which means I want to finish C,D,E from E and move to A or finish all activities and then open to a new instance of A. Please provide a solution if somebody already have a simple solution for this kind of problem. I tried many things and the closest I got was when I set launchMode of C to "singleTask" and launchMode of D to "singleInstance" and use intent to move to C when press back button, so that I can move back and forth without finishing any activity and it doesn't matter for E if new Instance is created or not, so kept it as "standard". Then I used intent to open A and and also added the flag "FLAG_ACTIVITY_CLEAR_TOP" and also tried to use startActivityFor result to close D since it's an "singleInstance" activity.

Now one of the problem is I'm not able to understand the structure which is being created but I think its something like this

Task 1 -- [A->C] (Since B is finished when C is opened)
Task 2 -- [D] (since singleInstance)
Task 3 -- [E]

Now I have to go from E to new instance of A and close everything else. Since all the activity have same affinity I tried using finishAffinity() but it didn't workout as well.

And the other problem is that the onActivityResult method in D is not called when you set results in E (According to one of the answers in stackoverflow I think it's because D is singleInstance), but it's being called from A when I press back button in A or we can say when finish() is called from A (I don't know why this happens). So, now there are different types of problems when I press back button.

1

There are 1 best solutions below

6
On

You can achieve your task using fragments. Make a function to replace fragment and with Boolean variables add and clear back-stack entries.