Event bus in fragment

I created one action (DemoActivity.java) with two fragments (FragmentOne.java and FragmentTwo.java).

I registered an EventBus in an Activity like this EventBus.getDefault().register(this);

and created one Suscriber method in Activity:

 @Subscriber public void abc(String str) { Log.i(TAG,"MainActivity Called !!"); } 

Then I dispatch the event from FragmentTwo.java when I click the EventBus.getDefault().post(""); button EventBus.getDefault().post("");

This script is great for me. But when I create the same subscriber method in FragmentOne.java, it does not work. Why?

+7
source share
1 answer

There may be two problems from your question:

  • You may not have registered EventBus in your FragmentOne class, as it was for your DemoActivity class.
  • If you registered an EventBus in the FragmentOne class, then check if the FragmentOne class is alive, and is able to receive the event when sending an event from the FragmentTwo class.

Edit

As you can see from the comments, you registered your EventBus as EventBus.getDefault().register(getActivity()) because of this, only your activity is logged. To register Fragment use EventBus.getDefault().register(this) Register EventBus.getDefault().register(this) in the Fragment.onCreate() method.

+5
source share

All Articles