Event Bus in Fragment
Asked Answered
B

3

19

I have created one Activity (DemoActivity.java) with 2 Fragments (FragmentOne.java and FragmentTwo.java).

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

and created one Suscriber method in the Activity:

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

Then I post an event from FragmentTwo.java on button click EventBus.getDefault().post("");

This scenario works fine for me. But when I create the same subscriber method in FragmentOne.java it is not working. Why?

Boyette answered 10/3, 2017 at 7:37 Comment(6)
You need to register the FragmentOne in EventBus like as same you are registering in activityNourishing
have you registered your fragment as event receiver?Overdone
@avi i alreay tried like - EventBus.getDefault().register(getActivity());Boyette
Why get Activity you need to pass the reference of fragment where you have define Subscriber method. Attach you code for better reference here.Nourishing
yes Vlad Matvienko. i used @Subscriber annotation for that.. is it right? or there will be another workaround for that?Boyette
@Avi got it.. i just passed 'this' instead of getActvity() and its works. thanksBoyette
T
17

From your question there might be two things that are causing the issue:

  1. You might have not registered the EventBus in your FragmentOne class like you did for your DemoActivity class.
  2. If you have registered the EventBus in the FragmentOne class then please check if FragmentOne fragment class is alive and in state to receive the event while posting the event from FragmentTwo class.

Edit

As seen from comments you have registered your EventBus as EventBus.getDefault().register(getActivity()) due to this only your Activity will get registered. To register your Fragment use EventBus.getDefault().register(this) in your Fragment.onCreate() method.

Tai answered 10/3, 2017 at 7:56 Comment(2)
yup just did it.. but when i will unregister that fragments eventbus ?Boyette
onDestroy() method of FragmentTai
C
9

Use Sticky Events for fragment. Because fragments are loaded multiple offsets some time.

Register and unregister your Eventbus:

 @Override
public void onStart() {
    Log.d(TAG, "Register ");
    EventBus.getDefault().register(this);
    super.onStart();
}

@Override
public void onStop() {
    super.onStop();
    Log.d(TAG, "Unregister");
    EventBus.getDefault().unregister(this);
}

OnChildChange.class post event with .postSticky():

EventBus.getDefault().postSticky(new OnChildChange(position));

Subscribe EventBus with sticky = true:

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onCategoryChangeEvent(OnChildChange event){
    // get the event and remove drom sticky
    OnChildChange stickyEvent = EventBus.getDefault().removeStickyEvent(OnChildChange.class);

    if(stickyEvent != null) {
        // apply your logic or call methods 
    }

}
Customer answered 13/4, 2018 at 14:4 Comment(0)
E
0

You have to Register EventBus at OnStart() in fragment.

  override fun onStart() {
    super.onStart()
    EventBus.getDefault().register(this)
}
Essential answered 15/12, 2021 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.