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!

4

2 回答 2

5

您实际上可以通过使用粘性事件来做到这一点。

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

基本上,您.postSticky()在 Activity B 上发布了一个粘性事件,当再次注册 Activity A 时,它将自动接收该粘性事件。

但正如@jlhonora 的回答中所述, startActivityForResult 可能更适合您的需要。

于 2016-02-06T03:29:19.310 回答
3

正如您已经知道的那样,您将无法使用 EventBus 在两个活动之间进行通信,因为您不能同时注册两个活动。

startActivityForResult 模式更适合您想要实现的目标:http: //developer.android.com/reference/android/app/Activity.html#StartingActivities

于 2015-09-04T16:24:36.597 回答