Autumn_Cat
Autumn_Cat

Reputation: 800

Android best practises: sharing data between fragments in ViewPager

The case: I use guide from androidhive to implement two tabs that by one activity. First one - list with complex objects in recyclerview, second - map with elements from this list. As DB I use SQLite but I'm going to migrate on nice and cozy Realm.

In SQLite case general approach which looks appropriate is to make list item object Parcelable and then use bundle to transfer data from activity to fragments. One call to the database, seems legit.

But in case of Realm I cannot use Parcelable because it requires getters/setters methods only.

What is the best way in this case?

Upvotes: 0

Views: 441

Answers (2)

Mina Wissa
Mina Wissa

Reputation: 10971

I belive you can make your model class implement Parcelable, it won't affect anything related to realm, you'll use the getters/setters along with the CREATOR object.

Another option is to use realm Parceler library, as mentioned in the docs, it generates the code required to make the objects parcelable so that you can pass them between activities or fragments.

Upvotes: 1

Christian Melchior
Christian Melchior

Reputation: 20126

You should only send an ID of the object you want to display and then fetch the object in the fragment as described here: https://realm.io/docs/java/latest/#intents

Upvotes: 2

Related Questions