Wednesday, December 4, 2024
HomeAndroidRxAndroid Tutorial #1 - Getting started with Reactive Extensions in Android

RxAndroid Tutorial #1 – Getting started with Reactive Extensions in Android

-

RxAndroid Tutorial Intro:

Reactive extensions usage is the current hottest trend in most programming languages like Java, JavaScript, Swift etc. Rx Android is the reactive extension for Android which is built on top of RxJava with minimum classes addition to it which makes it easier to add for Android Projects. This RxAndroid Tutorial is to help understand the usage of Reactive extensions in our Android Projects.

rxandroid tutorial

In this Introductory tutorial, we will be discussing about basic terminology and Addition of RXAndroid Framework library to our Android Project. So let us get started.

This article is part of RxJava Intro series. You can checkout the entire series here:

What is RxAndroid

RxAndroid is a framework to utilize the reactive extensions for Android Projects which is built on top of RxJava. RxAndroid is used to handle Asynchronous tasking efficiently in Android Applications.

What are Reactive Extensions (Rx)

Reactive Extensions (Rx) is a library for performing asynchronous programming with observable streams.

Data sequences exists in different forms such as stream of data from a file (or) user input (or) push notifications (or) web service requests etc.

Reactive Extensions represent all these data sequences as observable sequences which emits the data. Now an application can subscribe to these observable sequences to receive the new data asynchronously.

Related Articles:

Using Java 8 Features in Android – Android Java 8 Tutorial #1

Why Reactive Programming / RxJava?

With the increase of the Quad core, Octa core processors into Android World, our app would be butter smooth if we can harness the power of MultiThreading.

Android provides numerous concepts for achieving MultiThreading such as AsyncTask, Thread class, Runnable interface, ThreadPoolExecutor, etc., but extensive usage of these results in complex, error prone code.

RxJava and RxAndroid provides special Schedulers and operators to ease the conversion of this complex multithreading task into an easy way of specifying which thread your work needs to be done and which thread the results need to be updated. This concept is very much useful for android as we all know that we need to do tasks such as Networking etc. in background threads and our App’s UI updation related tasks in our Android’s Main thread.

RxJava2.0 is shipped with many new Schedulers like Scheduler.newThread() which creates a new thread to perform some work.

Basic Building Blocks for Rx Programming

There are 2 main building blocks for Reactive / Rx Programming

1. Observable:

Observables simply emits the data to those which subscribed to them. All the emission is done asynchronously to the subscribers.

Example code:

// RxAndroid Tutorial - Adding Observable
Observable<String> stringObservable = Observable.just("Hello Reactive Programming!");
2. Observer

Observer consumes the data emitted by the Observable. To do this, Observer needs to subscribe to the Observable.

// RxAndroid Tutorial - Adding observer
Observer<String> stringObserver = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(String s) {
                Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        };
3. Subscribers

Subscriber helps an Observer subscribe /unsubscribe from Observable.

// RxAndroid tutorial - observer subscribing to observable
stringObservable.subscribe(stringObserver);

Note:

An Observable emits data if there is at least one Observer subscribed to it.

Adding RxAndroid to our Android Project

Now we will add RxAndroid Library to our Android Studio Project. We’ll open our app module’s build.gradle file and add the following line of code to include RxAndroid to our Project

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'

However since RxAndroid’s releases are not as frequent as RxJava’s ones, we will add RxJava’s Library to our project in order to not miss the bug fixes and latest enhancements.

compile 'io.reactivex.rxjava2:rxjava:2.1.3'

In the next article, we will dig deeper along with an example code in Android Studio.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

LATEST POSTS

SOLID Principles in Android with Kotlin Examples

Recently I had an interview experience where I went blank when the interviewer posed an Interesting Question - Explain SOLID Principles and how they are...

Building a Multi Module App in Android | Modularization in Android #1

Recently, I was in requirement for developing a Multi Module app in Android and I was going through this great lecture on it. This article...

Lambda function in Kotlin with Examples

Lambda function is powerful feature in any Programming language and Lambda function in Kotlin is no exception. In this article, we will look at how...

Higher Order Functions in Kotlin with examples

There are many advanced features in Kotlin which gives an edge for the user using this language over Java. One such feature is Higher Order...

Follow us

1,358FansLike
10FollowersFollow
398SubscribersSubscribe

Most Popular