EventBus communication between two fragments shows No subscribers registered for event class..?

1.2k views Asked by At

First Time I'm trying to implement EventBus to communicate two fragments of MyActivity (to change button.setEnabled) my SDK 21

this is my POJO event

public class ButtonEvent {

public final boolean status;
public ButtonEvent(boolean status){

    this.status=status;
}

}

this is the fragment that 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 is the fragmentB which receives (listen) the event...

FragmentB extends Fragment{
...//some code
@Override
// in method onCreate I register the subscriber
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(!EventBus.getDefault().hasSubscriberForEvent(ButtonEvent.class)) {
        EventBus.getDefault().register(this);
    }

}
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_b, container, false);
//...some other code
}
//HERE IS MI SUBSCRIBER
@Subscribe
public void onEvent(ButtonEvent event){
    
    btnNuevoMed.setEnabled(event.status);
    btnNuevoMed.setText("hELLOOO");
}

this is my output but the app doesn't stop, but doesn't make any change on my fragmentB button:

D/EventBus: No subscribers registered for event class ar.com.titaves.consultoriosapp.servicios.ButtonEvent
D/EventBus: No subscribers registered for event class org.greenrobot.eventbus.NoSubscriberEvent

my eventbus version:

compile 'org.greenrobot:eventbus:3.0.0'

there are similar questions but some of them haven't got the right answer and the other are different situations Activity-fragment are not the same implementation than fragment to fragment.

Is my code right? I am missing something? Why it is not working and how can I solve the problem? thanks in advance...

1

There are 1 answers

2
Sachin Varma On BEST ANSWER

Here is a sample..

          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));
            }
});
}

        @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);
      }

Fragment B:

    FragmentB extends Fragment{
    ...//some code
    @Override
    // in method onCreate I register the subscriber
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_b, container, false);
    //...some other code
    }
    //HERE IS MI SUBSCRIBER
    @Subscribe
    public void onEvent(ButtonEvent event){

        btnNuevoMed.setEnabled(event.status);
        btnNuevoMed.setText("hELLOOO");
    }
 @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);
      }