2

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();
      }

 }
azurefrog
  • 10,785
  • 7
  • 42
  • 56
124697
  • 22,097
  • 68
  • 188
  • 315

4 Answers4

2

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

mVck
  • 2,910
  • 2
  • 18
  • 20
2

The problem is that you have to post the list with the post line of event bus and then you have to override onEvent() in your class. here is a link which will be helpful.

what you have to do is define additional events like

public static class MessageEvent { /* Additional fields if needed */ }

now from the part where you get the data you post it to the event.

EventBus.getDefault().post(new MessageEvent());

Inside you activity you place a subscriber function which will be triggered when data is posted to event bus. and there you use the posted data

public void onMessageEvent(MessageEvent event) {/* Use the data*/};
Arpan Sharma
  • 2,142
  • 11
  • 22
2

You will not get any event since you are in activity two, which means your first activity is in a "stopped" state. Depending on what you want to achieve, you can try startActivityForResult or create a sticky event, but I wouldn't recommend using event bus for simple operations like this since it can lead to nasty hard to debug errors.

So I recommend you go with option 1, use startActivityForResult.

AC-OpenSource
  • 346
  • 1
  • 12
-1

If you want to receive events on your Activity when your activity has no focus then you are probably doing something wrong. I would suggest moving the subscriber stuff to a static singleton class and from that class start the Activity with an Intent and some parameters when you have to manage some situation.

FrankMonza
  • 2,024
  • 16
  • 26