2

I have 2 fragments in an activity A, Frg1 and Frg2. Frg1 requires an object o1 as input and Frg2 requires o2 as input.

There is another activity B. So i start from Frg1 where I already have o1. So i populate Frg1 then i move to Frg2 where I get o2 and populate it. So in my fragment stack there are Frg1 and Frg2. then from Frg2 i move to activity B thus destroying activity A. So i loose o1 and o2 as well. On pressing back from activity B, I need to go to Frg2 then on back again I have to go to Frg1. To solve this I am passing o1 and o2 to activity B and on pressing back retrieving them on activity A. I want to know if this is the best way or is there any simple way to deal with this.

m.n Aswin
  • 119
  • 3
  • 7
  • are initializing objects in `Activity A`? – Junaid Hafeez Mar 01 '17 at 07:31
  • i fetch input objects from rest api calls. – m.n Aswin Mar 01 '17 at 08:25
  • 1
    make their scope global and check if they are null before API call. hopefully they won't destroy when you will back to your activity A. – Junaid Hafeez Mar 01 '17 at 08:37
  • 1
    use addToBackStack() to retain previous fragment. I have tested your scenario in one of my project it work fine I can navigate from ActivityB to Frag2 to frag1 on pressing back object are retained no use to do any work around – Rahul Mar 01 '17 at 09:35
  • Thank you guys.... i tried using bundle to retain objects while switching activities..... but i have around 16 activities and they all contain minimum 2 fragments.... so its taking a lot of time to save their states and all.... thanks again. – m.n Aswin Mar 01 '17 at 10:15

0 Answers0