I have a couple of applications that display downloaded content. So, data is get in XML via REST, and then parsed to ArrayList<MY_MODEL_CLASS>. Applications consist of ViewPagers with Fragments, they also have nested Fragments. Data is loaded when user opens corresponding fragments.
Currently all ArrayLists are usual class fields inside Fragments(private ArrayList<Event> downloadedEvents;). To prevent data loss and re-downloading after orientation change, fragments are set to setRetainInstance(true);. But child fragments doesn't allow to do that, so data is getting lost and re-downloaded.
Of course, I can save instance state, save ArrayList to Bundle as Parcelable, make ArrayList static, etc.
My question is more theoretic, that practic. I would like to know good practices of holding downloaded data inside application without writing it to persistent memory as SQLite.
My variants are:
- Store data in fragments, which display corresponding data, and save instance state to preserve ArrayList between orientation change.
- Store data in fragments, but just make fields static to preserve between object re-creation.
- Make one singleton object that stores all data for all fragments.
But I suppose, that none of them is really good.
What solutions (patterns) do professionals use?
Thanks! :)
Aucun commentaire:
Enregistrer un commentaire