2

The issue is, we have like ~10 fragments which needs to be shown on basis of backend. whenever we switch the fragment from one to another we need the state of that fragment to be stored. The fragments contain not only complex view logic as well as business logic too.

For example : we have 3 fragments f1,f2,f3.

we switched from f1 -> f2 by pressing next button in f1 state of f1 is added to backstack

we again switch from f2 -> f3 by pressing next button in f2 state of f2 is added to backstack

now, when we want to go to f2 from f3 which is done by pressing previous button in f3. as state was kept track. The state was returned as it by poppingbackstack

now comping to issue, again we want to switch from f2 -> f3 and we need f3 state to be reserved as f3 was visited before , when we try to do this the fragment is recreating and all view & business logic & states are getting lost.

Things Tried :

  1. Tried saving states to local preferences, but ended up taking huge chunk of memory & cpu power for saving & retrieving data bundle as data was huge which included views & some business logic too
  2. We created a singleton pattern which would save the fragment as list and iterate b/w them but instead of restoring the state, the fragment was recreating and was also resource heavy.
  3. Tried view pager , disabled swipe-left/right functionality, state was reserved could not switch the fragments within fragments, need to add the new fragments at first which could not be done as we get the list of fragments to be shown in fragments like 2-3. nearly most of the codebase needs to be changed which may alter a bit stability & functionality of application behavior.
nynohu
  • 1,628
  • 12
  • 12
VINAY DANARADDI
  • 195
  • 2
  • 12
  • Do all the fragments live in the same activity? – JDurstberger Jan 13 '17 at 07:06
  • share your code – Doctor Who Jan 13 '17 at 07:08
  • yes all fragments live in same activity. @Altoyyr – VINAY DANARADDI Jan 13 '17 at 07:20
  • @suneel the code base is large, could not share them here, – VINAY DANARADDI Jan 13 '17 at 07:20
  • check it here [Link](http://stackoverflow.com/a/41627514/1673458) if it sense something. I did something similar there. I had my data in an arrayList and i get position of that page and fetch that detail through index in array like `array[page_Index]`. So whenever I am doing any transition between fragment I am fetching the data from my array with fragment index number. – Doctor Who Jan 13 '17 at 07:24
  • @suneel that was the 2nd method i tried, the fragment will recreate/reload instead of retaining into its own state while the fragment was put into arraylist – VINAY DANARADDI Jan 13 '17 at 07:37
  • Have you tried using `viewPager.setOffscreenPageLimit(10);` – Doctor Who Jan 13 '17 at 07:44
  • @suneel i have tried them, and using viewpager will alter the stability of the application & will change the 90% core code base written for handling fragments, which is hard to implement now. – VINAY DANARADDI Jan 13 '17 at 08:06
  • 1
    If all the fragments are created in the same activity, is there the possibility to create a DataClass that contains all the information your fragments need and pass it into them when they get created? You can then read and write from the dataobject and the dataobject will live as long as the activity does. – JDurstberger Jan 13 '17 at 10:54

0 Answers0