0

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?

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
JoCuTo
  • 2,463
  • 4
  • 28
  • 44
  • Otto is deprecated by the way, use rxJava, really) – Beloo Apr 13 '18 at 09:14
  • thanks I can not , all is project is done with Otto :( – JoCuTo Apr 13 '18 at 09:14
  • General TIP: when subscribing to something in `onCreate()` remember to unsubscribe in `onDestroy()`. This also applies to `onCreateView()` -> `onDestroyView()`. – tymbark Apr 13 '18 at 09:39
  • 1
    Are you sure `FragmentC` has already gone through it's `onCreate()` at the time when you perform `EventBus2.post(number)`? – azizbekian Apr 13 '18 at 09:44
  • @azizbekian ok that´s the point FragmentC is not created at the time that I am performing the post method ... – JoCuTo Apr 13 '18 at 10:01
  • Use handler to post an action after some time. Or use a sticky event posting (`bus.postSticky(...)`), this has a side effect after performing an orientation change in `FragmentC`. – azizbekian Apr 13 '18 at 10:04

0 Answers0