Android: with Greenrobot EventBus how to communicate between 2 Activity?
Asked Answered
D

2

5

I would like to communicate between 2 Activity. Both are the register() and the unregister() methods:

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

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

The ActivityB is started when I click in my list (item selection). The aim of ActivityB is to update some informations, and to send these new informations to ActivityA; so in ActivityB I call:

EventBus.getDefault().post(new MyNewEvent(bla bla bla));

In my ActivityA I have this method:

public void onEvent(MyNewEvent event) {
    ...
}

Unfortunately this method onEvent is never called. Why ? Because when ActivityB starts the method onStop() in ActivityA is called, so the unregister with the bus is done...

So how to communicate in this case between these 2 Activity by using EventBus ?

Thank you guys!

Duron answered 4/9, 2015 at 16:13 Comment(0)
C
3

As you already figured out, you won't be able to use EventBus to communicate between two activities, since you can't have both registered for events at the same time.

The startActivityForResult pattern is much better suited for what you're trying to achieve : http://developer.android.com/reference/android/app/Activity.html#StartingActivities

Corvese answered 4/9, 2015 at 16:24 Comment(3)
Thanks for your explanation. So Event bus is not really the ultime choice to communicate between Android Component.Duron
It depends. I like it a lot when I have many non-critical events, or when communicating with background/async tasks. But when doing inter-activity communication, intent/results is the way to go.Corvese
If we can't use EventBus to communicate between Activities, then write your simple EventController just for handling events in all Activities, Fragments. No need to use EventBus at all. Create EventController class. Use CopyOnWriteArrayList to store EventListener references. And just implement EventListener interface in all Activities. EventListener interface will contain a single method named handleEvent().Timoshenko
N
5

You can actually do that by using Sticky Events.

http://greenrobot.org/eventbus/documentation/configuration/sticky-events/

Basically, you post an event as sticky with .postSticky() on your Activity B and when Activity A is registered again, it will automatically receive that sticky event.

But as stated in @jlhonora's answer, startActivityForResult might be better suited for your need.

Nikianikita answered 6/2, 2016 at 3:29 Comment(0)
C
3

As you already figured out, you won't be able to use EventBus to communicate between two activities, since you can't have both registered for events at the same time.

The startActivityForResult pattern is much better suited for what you're trying to achieve : http://developer.android.com/reference/android/app/Activity.html#StartingActivities

Corvese answered 4/9, 2015 at 16:24 Comment(3)
Thanks for your explanation. So Event bus is not really the ultime choice to communicate between Android Component.Duron
It depends. I like it a lot when I have many non-critical events, or when communicating with background/async tasks. But when doing inter-activity communication, intent/results is the way to go.Corvese
If we can't use EventBus to communicate between Activities, then write your simple EventController just for handling events in all Activities, Fragments. No need to use EventBus at all. Create EventController class. Use CopyOnWriteArrayList to store EventListener references. And just implement EventListener interface in all Activities. EventListener interface will contain a single method named handleEvent().Timoshenko

© 2022 - 2024 — McMap. All rights reserved.