EventBus does not update textview or button and does not show any errors?

824 Views Asked by At

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?

1

There are 1 best solutions below

3
On

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){
    //