0

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.

anubh
  • 649
  • 7
  • 12

1 Answers1

0

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

Khizar Hayat
  • 3,427
  • 3
  • 18
  • 22
  • I'm already using many fragments inside each activities with many UI elements, so its unnecessarily gets complicated when I try to use too many fragments in one activity. – anubh Feb 25 '16 at 13:56
  • Ok. You can use BaseActivity everyActivity extend by BaseActivity. in Base you can track which activity is on foreGround and which one is comming using some hashTable or any data structure which suits you. – Khizar Hayat Feb 25 '16 at 13:58
  • I don't get it, since by using Activity life cycle methods also we can keep a track of which activity is in foreground and which is coming. Can you give some example so that I can understand it better. – anubh Feb 25 '16 at 14:09
  • I am not talking about activity life cycle. i suggest that if you can do it manualy . like when you startActivtiy push that acitivty class name or some tag track in custom List . and pop when you finish it. – Khizar Hayat Feb 25 '16 at 14:14
  • That is what I'm saying, I know which activity is running and which activity is not it's just I'm not able to finish these activities when I want because of the above mentioned reasons. – anubh Feb 25 '16 at 14:25
  • ok sorry about that . hope this link helps you http://stackoverflow.com/questions/10379134/finish-an-activity-from-another-activity – Khizar Hayat Feb 25 '16 at 14:30