I have a variable in a Fragment that will have its value change multiple times throughout the Fragment's life. It's triggered by UI interactions, so I thought it might be a good idea to use an Observable to hold it, rather than make all of the to-be-updated views as fields and do my UI changes from a setter.
The value has to be updated through another method (basically a setter that should call onNext()
on the subscriber), not through the Observable itself. Is there a way to do that with RxJava's design?
In other words, I'm looking to have an Observable field, and give it new values to emit (calling onNext()
on its subscribers) from another method in the class.
RxJava
has Subjects
for this purpose. For example:
private final PublishSubject<String> subject = PublishSubject.create();
public Observable<String> getUiElementAsObservable() {
return subject;
}
public void updateUiElementValue(final String value) {
subject.onNext(value);
}
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