Otto subscribe and post in the same fragment does not work

73 Views Asked by At

I have three fragments in the same activity, let says FragmentA, FragmentB and FragmentC

Let´s say that FragmentA sends an int to FragmentB so in the FragmentA I have:

    //FRAGMENT_A
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getBus().register(this);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getBus().register(this);
    }
   private void registerEventBus(int i) {
        EventBus.getBus().post(i);
    }

FragmentB received the int so:

  //FRAGMENT_B
  @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getBus().register(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getBus().unregister(this);
    }
      @Subscribe
    public void getBusData(int data) {
       //whatever
    }

But now I have to send a double from FagmentB to FragmentC, I have created a new Bus class so now in the FragmentB, I have

//Fragment_B
       @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            EventBus.getBus().register(this);
            EventBus2.getBus.register(this)
        }

        @Override
        public void onDestroyView() {
            super.onDestroyView();
            EventBus.getBus().unregister(this);
            EventBus2.getBus..unregister(this)
        }
          @Subscribe
        public void getBusData(int data) {
           //whatever
        }
        private void createEventBus2(double number){
         EventBus2.post(number);
        }

And finally the FragmentC

//FragmentC
 @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus2.getBus().register(this);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus2.getBus().unregister(this);
    }
      @Subscribe
    public void getBus2Data(double data) {
      //whatever
    }

From FragmentA to FragmentB works perfectly but in the FragmentC I have never get the double data value.

What am I doing wrong?

0

There are 0 best solutions below