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.