事件总线 - 创建多个事件

问题描述 投票:0回答:1

如何使用事件总线创建多个事件?

所以在这个例子中,我试图接收多个事件。但是当 addEvent3WhenLogicOccers() 的值,称为 addEvent1WhenLogicOccers()new EventBusEvents() 在活动公交车岗位上。我想同时收到 EVENT_1EVENT_3 与他们的数据是 my_event_child_1my_event_child_3. 怎样才能做到这一点?

String EVENT_1 = "my_event_child_1";
String EVENT_2 = "my_event_child_2";
String EVENT_3 = "my_event_child_3";

addEvent1WhenLogicOccers() {
    EventBus.getDefault().postSticky(new EventBusEvents(EVENT_1));
}

addEvent3WhenLogicOccers() {
    EventBus.getDefault().postSticky(new EventBusEvents(EVENT_3));
}

@Subscribe(sticky = true)
    public void onEvent(EventBusEvents eventBusEvents) {
        switch(eventBusEvents.eventChilds) {
               case EVENT_1:
                   // on event 1 code
                   break;
               case EVENT_2:
                   // on event 2 code
                   break;
               case EVENT_3:
                   // on event 3 code
                   break;
        }
}
android event-bus greenrobot-eventbus
1个回答
0
投票

我发现 EventBus 对我来说不是最好的解决方案。我用接口代替 这里

© www.soinside.com 2019 - 2024. All rights reserved.