Debu Debu - 1 year ago 114
Android Question

Detecting value change using Rxjava

Can we detect if a class member value is getting changed using RxJava?? Say in a class there is a variable var, now can we get notified whenever the value of var changes using RxJava.

Answer Source

You can use something like this:

private final BehaviorSubject<Integer> subject = BehaviorSubject.create();
private Integer value=0;

public Observable<Integer> getUiElementAsObservable() {
    return subject;

public void updateUiElementValue(final Integer valueAdded) {
    synchronized (value) {
        if (value + valueAdded < 0)
        value += valueAdded;

and subscribe to it like this:

            .subscribe(new Action1<Integer>() {
                public void call(Integer userMessage) {

you have to create setter for all of your variables that you want something subscribe to their changes and call onNext if change applied.


some useful link: about subject in rx: about reactive programming : and about rxjava :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download