How do I send data from Fragment to Fragment with EventBus?

431 Views Asked by At

I am using EventBus to send the long value from a fragment to another fragment. I use the following code to do that. But for me, it doesn't work. What did I do wrong?

This is the fragment where I save the data:

@OnClick(R.id.buttonFinishMeeting)
public void onClickButton() {
    startActivity(RoutePlanCompleteActivity.newIntent(getContext(), routePlan));
    EventBus.getDefault().post(new Long(spentTime));
}

Then the fragment I want to send my data:

@Subscribe
public void onEvent(Long time) {
    spentTime = time;
}

@Override
public void onResume() {
    super.onResume();
    EventBus.getDefault().register(this);
}

@Override
public void onPause() {
    super.onPause();
    EventBus.getDefault().unregister(this);
}
1

There are 1 best solutions below

2
On

Replace your subscribe method like this :-

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(MessageEvent event) {
---
};

This code has been taken from the official documentation of Eventbus from here. If you have any more trouble, visit this link https://github.com/greenrobot/EventBus