简体   繁体   中英

Otto - subscribe multiple events in the same Fragment/Activity

I am using Retrofit with Otto. My problem is how to subscribe to multiple events in the same Fragment(or 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); .

Also I can't subscribe twice, like: @Subscribe public void getDataOne(Event1 event1); and @Subscribe public void getDataTwo(Event2 event2); in the same Fragment(or Activity) class.

In my Fragment class I register and unregister the bus:

@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:

 @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). Here this is not a big deal anymore, I can subscribe multiple Events in the same Fragment/Activity using the same manner. Anyway, I'll put here some code for beginners who have the same issue.

So, the first we need to declare library into gradle:

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

Then create a generic class for all project, with EventBus instance:

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:

(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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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