0

I´m trying to pass data from a fragment to ViewPager slides Fragments. My Activity xml is divided in 2 halves. 1 Fragment for input with button listener. 3 Fragment Slides for output. What I want to do is pass data from the Fragment (input) to all 3 Fragment slides (output), So I can swipe through the data I calculated.

I tried to use an interface to pass data from Fragment (input) over Activity to the Fragment slides. My App crashes once I press on the button listener to send data. I get a NullPointerException.

What´s the best or easiest way to make this work? I am really stuck with this problem.

Thx.

2 Answers2

0

I think you should just keep a reference of each fragment by adding a helper code in your FragmentPagerAdapter. Each of your fragment should implement an interface. Whenever required check if instance is not equal to null then call the interface method to pass the data.

Below code will help you to keep the reference of the fragment. https://stackoverflow.com/a/29269509/4901098

Community
  • 1
  • 1
Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20
  • Yes I was suggesting a quick fix not considering the design. If one needs to think wrt design then he might use a observer pattern here. Use a data model to put the data which is being used in the first fragment. This model would be Observable and whichever fragment is interested in observing the change can be the observer and change its view accordingly. – Arpit Ratan Jun 12 '16 at 14:36
0

Try this approach: Add the following to your module level build.gradle file

compile 'org.greenrobot:eventbus:3.0.0'

Then, create a regular java class that represents your Event.

public class UpdateFragmentsUIEvent{

   //add your values you want to pass to your other fragments
   private String valueA;
   private String valueB;
   private String valueC;
   //.....others?

   public UpdateFragmentsUIEvent(){
   }

   //add getters and setters here 
}

Now that you have created your event class, in the fragment that you want to update (not the first one, but the ones you want to pass the data to), do the following:

public class FragmentB extends Fragment{

   @Override
   public void onAttach(Context context){

      super.onAttach(context);

      //important here - could be done inside onCreate()
      EventBus.getDefault().register(this);
   }

   //very important here as well
   public void onEvent(UpdateFragmentsUIEvent event){
      /* this event instance has all your values here now */

      String valueA = event.getValueA();
      String valueB = event.getValueB();

      /* Now you can update your views accordingly */
   }

   /* don't forget to unregister inside onDestroy */
   @Override
   public void onDestroy(){

      super.onDestroy();
      EventBus.getDefault().unregister(this);
   }
}

Finally, to actually notify your fragments, you have to post your newly minted event like this inside the first fragment I suppose is where you enter values :

//somewhere when a button is clicked after entering data
EventBus.getDefault().post(new UpdateFragmentsUIEvent(valueA, valueB, valueC));

And just like that, you are done! Forget about the interfaces and stick to recommended design patterns that follows loose coupling of code like the publisher/subscriber pattern used in this example.

I hope this helps and please let me know if you need more help!

Eenvincible
  • 5,641
  • 2
  • 27
  • 46