I'm trying to send a list of objects from ActivityTwo
to MainActivity
I have followed EventBus's get started page and called register()
and unregister()
methods from onStart and onStop, then i used EventBus.getDefault().post()
to send the data.
On MainActivity
I do not get anything unless I remove the unregister()
call from onStop()
.
My question is, am I using it correctly? should I be calling unregister()
in onDestroy()
? if so, why are they calling it from onStop
if it will not pick up anything if the activity is stopped
MainActivity
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(YoutubeData event) {
System.out.print(event);
}
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
ActivityTwo
private void sendYoutubeData() {
if(youtubeData != null){
EventBus.getDefault().post(youtubeData);
finish();
}
}
If you want to receive the event in MainActivity when it's not visible, yes, you should place your register()
and unregister()
respectively in onCreate()
and onDestroy()
.
If there's no listeners for your event, it will never be received, EXCEPT if it's a sticky event, in which case you will be able to receive it when registering. More information about Sticky Events here