5

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?

1

1 Answer 1

2

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(...);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, that helped. But now I am still a bit unsatisfied. Because I have to use the Application class as a "passing interface" and I would like to avoid that. As I heard RxJava can be used to send events on an EventBus. How to achieve this?
@unlimited101, in most implementations event bus is basically a simple observable. You could put it in a static variable instead of an Application and make a wrapper around it. There's no other way to share an object across activities as far as I'm aware.
By eventbus (github.com/greenrobot/EventBus) you can send data between Android components like Services, Activities, etc. But I would like to realize this in RxJava.
@unlimited101 if you look at the code, for example EventBus.java, you'll notice that it's implemented as a singleton and its instance is stored in a static variable. You could make something similar by making a wrapper around your Observable which would be instantiated as a static variable.
in most case your event bus will be either a field of the application, a static variable somewhere, or an injected singleton (I'd go with the latter, obv., but others are fine if you don't use dependency injection in your project)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.