RxJava and random sporadic events on Android

advertisements

I want to use RxJava like I would used Guava's EventBus or Otto, but I don't see how I can get it to function that way.

This is the scenario: let's say I want to have a button in my Android app and every time a button is pressed I want RxJava to emit an event via my Observable. It seems to me that I have to have the service reregister after it gets an event and that the activity would need to create a new observable as well.

Like if I say

Observable.from(x)

seems to me I would need to that for every event, but that creates a new observable that would need to be registered to again. Surely I am missing something.


You may want to do something like this (from rx.subjects.PublishSubject):

PublishSubject<Object> subject = PublishSubject.create();
// observer1 will receive all onNext and onCompleted events
subject.subscribe(observer1);
subject.onNext("one");
subject.onNext("two");
// observer2 will only receive "three" and onCompleted
subject.subscribe(observer2);
subject.onNext("three");
subject.onCompleted();

If you could inject the Subject interface into the Service and the PublishSubject into the Activity (or vice versa depending on what your doing) you can have a good separation of concerns.