I have a MainActivity
with three separate fragments. I need to access data from my SQLite database in each of these fragments.
The data object MyObject
contains primitive types along with a list of OtherObject
objects.
So lets say we have three fragments, FragmentA
, FragmentB
and FragmentC
.
FragmentB
creates MyObject
objects and saves them into the database. Upon saving, FragmentC
is opened and populated with the MyObject
data.
FragmentA
lists all MyObject
objects and upon clicking on an item from the list, will open FragmentC
populated with the chosen MyObject
.
What is the correct implementation?
Should the activity control all the database access and pass MyObject
to the corresponding fragment?
Should each fragment access the database each time to access the data for itself?
Should FragmentA
and FragmentB
read in the data and pass the corresponding MyObject
to FragmentC
?
For the case of passing data from fragment -> activity -> fragment
or activity -> fragment
, which method would fit best:
- Parcelable
- Intent
- Interface (via the activity)
The use of parcelable seems difficult as MyObject
contains a list of OtherObject
, and all the data can't be decomposed and transferred in an Intent very easily.
This link shows the use of an Interface to transfer data from the fragment to the activity, with the Fragment declaring the interface which is implemented in the activity. To transfer the objects from the activity to the fragment, do you declare another interface in the activity and implement it in the fragment?
I haven't been able to find any straight forward advice.
Thanks for any help
Aucun commentaire:
Enregistrer un commentaire