I am just doing my first steps with RxJava, making use of this tutorial. I understand what an Observable and what a Subscriber is. But I have problems with "connecting" them. I just thought about such a task: An Activity A starts an Activity B. Activity B has a button with which you can create a Date containing the current date. Another button emits this Date and finishes Activity B. Activity A subscribes to the emitted Date-Observable and displays the current date. (I know this is normally done by using Activity results). I have the following problem:
I can create a Date-Observable within Activity B by this code(The instance mDate is of type Date and is created somewhere else in Activity B):
Observable<Date> dateObservable =
            Observable.create(sub -> {
        sub.onNext(mDate);
        sub.onCompleted();
    });
But in Activity A I have to subscribe to it. But I have no reference to the Observable in Activity B. I thought about creating the Observable statically in my Application class but there I don't know the value of mDate yet. Can I somehow create an Observable of a certain type without implementing the "call()"-Method (where onNext() and onCompleted() are called)? So then I would already have an Observable instance I could subscribe to in my Aativity.And then later I could implement the "call()"-Method in Activity B? Ore are there other RxJava features I can use to achieve my described goals?
You need a thing called ReplaySubject.
In your custom MyApplication class:
ReplaySubject<Date> dateSubject = ReplaySubject.create();
In activity B:
ReplaySubject<Date> subject = ((MyApplication)getApplication()).getSubject();
subject.onNext(mDate);
subject.onCompleted();
In activity A:
ReplaySubject<Date> subject = ((MyApplication)getApplication()).getSubject();
subject.subscribe(...);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With