Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication objects between multiple fragments in ViewPager

I have 5 fragments in ViewPager used to fill business object with several fields step by step, in each step some of those fields will be set. I've read many articles about communication between fragments but I'm not feeling comfortable the way others preferred, so after thinking about HOW should I do this in my case, finally I start thinking to use singleton model object which all fragments can easily access to its fields and fill them in specific steps.

As I'm new to android I want to hear from experts about using singleton instead of passing data between fragments such as implemented interface(It seems its so complicated and hard to maintenance). Any advice will be helpful.

like image 312
saber Avatar asked Feb 17 '17 16:02

saber


People also ask

Why two fragments should never communicate directly?

Two Fragments should never communicate directly. The reason for this is that Fragment s are fluid & dynamic UI components that may fade in and out of view. Only the hosting Activity is capable of determining if a Fragment is added to the UI or has been detached from it.


1 Answers

While singleton approach seems easy to implement and understand it is way not to best way to achieve what you need. One reason is that your model object or as you call it business object lives outside of your activity's context which can create hard to find bugs. E.g. in case when more than one instance of your activity class is created by system and both keep reference to your singleton. See how you lose track of your objects?

What I would do is

  1. Make my model object to implement Parcelable you will hate it at the beginning but once you get use to it it will become your model's best friend
  2. Since your model is parcelable now you can easily pass it between fragments, activities, and even save it in shared preferences. One important thing to note here when you pass your parcelable between fragment or activity it is like pass by value, i.e. every time new instance is created.
  3. Set your fragment's argument or if it is already instantiated then get arguments and add your model. here is an example: if a fragment is not active yet:

    Bundle args = new Bundle(); args.putParcable("businessObject", yourBusinessObjectThatIsParcable); yourFragment.setArguments(args);

    Otherwise: yourFragment.getArguments().putParcelable("businessObject", yourBusinessObjectThatIsParcable);

  4. In your fragment perhaps in onCreateView method get your model object like this MyParcableObject mpo = (MyParcableObject)getArguments().getParcelable("businessObject") and use it set whatever data you want.

  5. When you finish editing your object on button click or in onPause method updated your fragment's arguments same way getArguments().putParcelable("businessObject", mpo);

  6. in your last page or last fragment you can pass your object to your activity, here is how to do it

Even though it looks cumbersome but it is a practice that you need to get used to as an android developer. You get lot more control when your model implements parcelable.

Another way to do what you need is thru Delegation Pattern but it is mostly used for callbacks even though you can pass objects as well.

like image 106
Vilen Avatar answered Sep 28 '22 01:09

Vilen