I am just switched from using Otto to RxAndroid for event bus usage mainly.
On Otto I was able to use @produce to get the last value of an event, in case an activity/fragment wasn't subscribed yet. I cant figure out how this is done by using RxAndroid.
I need this so I can receive an event from activity/fragment a to activity/fragment b.
Using a singleton instance of this class over the entire app
public class RxBus {
private final Subject<Object, Object> bus = new SerializedSubject<>(PublishSubject.create());
public void send(Object object) {
bus.onNext(object);
}
public Observable<Object> toObserverable() {
return bus;
}
}
sending data from ActivityA.java
@Inject RxBus bus;
..
bus.send(new DataEvent());
..
to ActivityB.java
..
CompositeSubscription subscription = new CompositeSubscription();
ConnectableObservable<Object> dataEventEmitter = bus.toObserverable().publish();
subscription.add(dataEventEmitter.subscribe(new Action1<Object>() {
@Override
public void call(Object o) {
Toast.makeText(context, "DataEvent", Toast.LENGTH_SHORT).show();
}
}));
subscription.add(dataEventEmitter.connect());
..
But this isn't receiving any event tho. I thought using ConnectableObservable
should work but doesn't.
I also found out using
private final Subject<Object, Object> bus = new SerializedSubject<>(BehaviorSubject.create());
instead of
private final Subject<Object, Object> bus = new SerializedSubject<>(PublishSubject.create());
Does receive the last event. But this is not always the needing case through out the entire app. Couldn't find any examples so I am asking for help.