简体   繁体   English

Otto - 在同一个 Fragment/Activity 中订阅多个事件

[英]Otto - subscribe multiple events in the same Fragment/Activity

I am using Retrofit with Otto.我正在与 Otto 一起使用 Retrofit。 My problem is how to subscribe to multiple events in the same Fragment(or Activity).我的问题是如何订阅同一个 Fragment(或 Activity)中的多个事件。 According to the official doc "The method should take only a single parameter, the type of which will be the event you wish to subscribe to."根据官方文档“该方法应仅采用单个参数,其类型将是您希望订阅的事件。” :

I can't do @Subscribe public void getAllData(Event1 event1, Event2 event2);不能@Subscribe public void getAllData(Event1 event1, Event2 event2); . .

Also I can't subscribe twice, like: @Subscribe public void getDataOne(Event1 event1);也不能订阅两次,比如: @Subscribe public void getDataOne(Event1 event1); and @Subscribe public void getDataTwo(Event2 event2);@Subscribe public void getDataTwo(Event2 event2); in the same Fragment(or Activity) class.在同一个片段(或活动)类中。

In my Fragment class I register and unregister the bus:在我的 Fragment 类中,我注册和取消注册总线:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    BusProvider.getInstanceBus().register(this);
}

@Override
public void onDetach() {
    super.onDetach();
    BusProvider.getInstanceBus().unregister(this);
}

Using generic class:使用泛型类:

  public class BusProvider {
    private static final Bus BUS = new Bus();

    public static Bus getInstanceBus(){
        return BUS;
    }

    private BusProvider(){}
}

I post my event from success() method of my retrofit request:我从我的改造请求的success()方法发布我的事件:

 @Override
        public void success(DataResponseOne dataResponseOne, Response response) {
            Log.d(GeneralConstants.LOG_TAG, " !SUCCES!");

            //sent data to otto bus
            BusProvider.getInstanceBus().post(dataResponseOne);

[some code for to get json in string]

            Log.d(GeneralConstants.LOG_TAG + "  !SUCCES!" + resultJSON );
        }

and the same for the second event:第二个事件也是如此:

    @Override
        public void success(DataResponseTwo dataResponseTwo, Response response) {
            Log.d(GeneralConstants.LOG_TAG, " !SUCCES!");

            //sent data to otto bus
            BusProvider.getInstanceBus().post(dataResponseTwo);

[some code for to get json in string]

            Log.d(GeneralConstants.LOG_TAG + "  !SUCCES!" + resultJSON );
        }

I suppose there is some tricky that I miss.我想我想念一些棘手的事情。 Any advices will be appreciated.任何建议将不胜感激。

Actually I was changed my library from Otto to EventBus (com.google.common.eventbus).实际上,我的库从Otto更改为EventBus (com.google.common.eventbus)。 Here this is not a big deal anymore, I can subscribe multiple Events in the same Fragment/Activity using the same manner.在这里,这不再是什么大问题,我可以使用相同的方式在同一个 Fragment/Activity 中订阅多个事件 Anyway, I'll put here some code for beginners who have the same issue.无论如何,我会在这里为遇到相同问题的初学者提供一些代码。

So, the first we need to declare library into gradle:所以,首先我们需要将库声明到 gradle 中:

compile group: 'com.google.guava', name: 'guava', version: '15.0'

Then create a generic class for all project, with EventBus instance:然后为所有项目创建一个通用类,使用 EventBus 实例:

public class EventBusGenerics {

        private static EventBus 

EVENTBUS = new EventBus();

public static EventBus getInstanceEventBus() {
    return EVENTBUS;
}

private EventBusGenerics() {
    }
}

Post your events into the buss:将您的活动发布到总线中:

(first one) (第一)

@Override
        public void success(DataResponseOne dataResponseOne, Response response) {
            Log.d(GeneralConstants.LOG_TAG, " !SUCCES!");

            //sent data to otto bus
            BusProvider.getInstanceBus().post(dataResponseOne);

[some code for to get json in string]

            Log.d(GeneralConstants.LOG_TAG + "  !SUCCES!" + resultJSON );
        }

(second one) (第二个)

@Override
        public void success(DataResponseTwo dataResponseTwo, Response response) {
            Log.d(GeneralConstants.LOG_TAG, " !SUCCES!");

            //sent data to otto bus
            BusProvider.getInstanceBus().post(dataResponseOne);

[some code for to get json in string]

            Log.d(GeneralConstants.LOG_TAG + "  !SUCCES!" + resultJSON );
        }

Register and unregister Fragment/Activity where we need this event:在我们需要此事件的地方注册和注销 Fragment/Activity:

(here is for fragment) (这里是片段)

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    BusProvider.getInstanceBus().register(this);
}

@Override
public void onDetach() {
    super.onDetach();
    BusProvider.getInstanceBus().unregister(this);
}

and subscribe the events, both of them :和订阅的事件,他们两个

@Subscribe
    public void onSubscribeLoginResponse(DataResponseOne dOne) {
[code to use the event...]
}

@Subscribe
    public void onSubscribeLoginResponse(DataResponseTwo dTwo) {
[code to use the event...]
}

You can have both data responses extend the same base class which your subscriber takes as a parameter.您可以让两个数据响应扩展您的订阅者作为参数的相同基类。 You can then use instanceOf to see if it is Event1 or Event2 .然后您可以使用 instanceOf 来查看它是Event1还是Event2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM