Reactive Hello world application in android

Adding RxJava 2 library  to a Java project in Android Studio

To use RxJava in a Gradle build, add the following as dependency.

compile group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.1.1'

For Maven, you can add RxJava via the following snippet.


    io.reactivex.rxjava2
    rxjava
    2.0.4

STEP 1:

Declare an observable data items first

Observable observable = Observable.just("Hello ", " World");

STEP 2

Create an Observer like this

Observer stringObserver = new Observer() {
    @Override
    public void onSubscribe(Disposable d) {
        Log.d(TAG, "onSubscribe() called with: d = [" + d + "]");
    }

    @Override
    public void onNext(String s) {
        Log.d(TAG, "onNext() called with: s = [" + s + "]");
        appCompatTextView.setText(String.format("%s%s", appCompatTextView.getText().toString(), s));
    }

    @Override
    public void onError(Throwable e) {
        Log.d(TAG, "onError() called with: e = [" + e + "]");
    }

    @Override
    public void onComplete() {
        Log.d(TAG, "onComplete() called");
    }
};

There Are four methods implement when create an Observer

  • onSubscribe(Disposable d) 
  • onNext(String s) Send a new value to a subscriber
  • onError(Throwable e) Notify observer about an error that has occurred
  • onComplete() Let subscriber know that it should unsubscribe as there won’t be any more content from this observable.

When Subscribing  onSubscribe called first time that observer is subscribed observable data,After that onNext called twice returning string items,When completed items onComplete called and transaction completed. If any error occurs onError called .

STEP 3:

Subscribe Observable Using Observer created above.

observable.subscribe(stringObserver);

After Subscribing onNext(String s) called two times  because we put two string items in observable

in onNext(String s)  we setting text to a textview.

 

 

Get Full Source code From Github

Thanks Happy coding 😀

 

Categories: RxJava2

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Social Media Auto Publish Powered By : XYZScripts.com