0

I am communicating two fragments of MyActivity when the event is fired from FragmentA (on button click) I want to change in FragmentB button status to enabled true and textview setText("new text") which are in FragmentB, when I run my app output shows no errors but does not make any changes.

here is my fragmentA which fires the event:

    Fragment A
        ...//some code
         @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_a, container, false);
        ...//more code inside onCreateView
          btnChange.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
        //HERE I POST THE EVENT
                    EventBus.getDefault().post(new ButtonEvent(true));
//   HERE I PUT SOUT TO SHOW IF EVENT IS FIRED
                    System.out.println("YOU FIRED THE EVENT");// THIS MSG IS SHOWN CORRECTLY
        }
      });
}

    @Subscribe
    public void onEvent(ButtonEvent event){

    }

@Override
  public void onStart() {
    super.onStart();
    if (!EventBus.getDefault().isRegistered(getActivity())) {
      EventBus.getDefault().register(this);
    }
  }

  @Override
  public void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
  }

here is FragmentB

 FragmentB extends Fragment{
...//some code

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_b, container, false);
 btnNuevoMed= (Button) rootView.findViewById(R.id.btnNuevoMed);
    btnNuevoMed.setEnabled(false);
    txtMed= (TextView)rootView.findViewById(R.id.txtMed);
//...some other code
}
//HERE IS MI SUBSCRIBER
@Subscribe
public void onEvent(ButtonEvent event){
// HERE I PUT A SOUT TO SHOW IF REACH THE METHOD
 System.out.println("YOU ARE HEREEEEE");  //BUT NEVER REACH THIS METHOD . WHY?
    btnNuevoMed.setEnabled(event.status);
    btnNuevoMed.setText("hELLOOO");
    txtMed.setText("Modification Success");
}
 @Override
  public void onStart() {
    super.onStart();
    if (!EventBus.getDefault().isRegistered(getActivity())) {
      EventBus.getDefault().register(this);
    }
  }

  @Override
  public void onStop() {
    super.onStop();
    EventBus.getDefault().unregister(this);
  }

this is my button xml

 <TextView
        android:id="@+id/txtMed"
        android:text="MEDICACION"
        style="@style/textoTitulos"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"/>

 <Button
        android:text="Nuevo Boton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnNuevoMed"

        />

I'm using eventbus 3.0.0. What is wrong with my code? How can I solve this problem? Why can't I reach the listener method?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
matQ
  • 597
  • 2
  • 13
  • 27

1 Answers1

0

Try this:

In Fragment A post event like this

EventBus.getDefault().postSticky(new ButtonEvent(true));

In Fragment B register subscribe like this:

@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onEvent(ButtonEvent event){
    //
Embydextrous
  • 1,611
  • 1
  • 12
  • 20
  • thanks, i try your code but it crush the outputs show this error: org.greenrobot.eventbus.EventBusException: Subscriber class ar.com.titaves.consultoriosapp.views.fragmentsPaciente.FragmentB already registered to event class ar.com.titaves.consultoriosapp.servicios.ButtonEvent . In the method onStrart of my FragmentB, can you help me? thanks – matQ Aug 03 '17 at 01:49
  • Do one thing. In your original code in Fragment B use !EventBus.getDefault().isRegistered(this) instead of getActivity(). Remove sticky thing. – Embydextrous Aug 03 '17 at 02:07
  • I/System.out: YOU ARE HERE I/System.out: YOU FIRED THE EVENT. this is my new output so now it get in FragmentB subscriber first and show its msg, and second the sout of the FragmentA, that means they are executing in invert order, however ir gets into the FragmentB subscriber method it does not change either the button and txtview – matQ Aug 03 '17 at 02:37