RxJava2 in Android Part2

In my last tutorial, We learned basic of Rx Observable and Observer. We learned how Observable and observer works together. If you still not checked then please visit here to get details: RxJava2 in Android Part1.In this article, I am focusing about Rx (Reactive programming). So question now What is Rx? Rx means Reactive Extensions It means anything change happens it will react something. Rx Functionality is hard to understand initially if you from basic java, But believe me it is really awesome because it gives the good result. Let's check the basic hello world programming in Android with Rx. Rx is basically standard Observer pattern. Observable emit the data and Observers consume the data. To emit the data Observers should subscribe.

In general android programming, we used many callback and event to complete the task. It's really hard to understand for beginner developers. Even I also not like personally to used many callbacks.For example, We have a fragment that contains listview or recycler view, In adapter class, we used callback while click on any item view to update something.Let me take another example in simple word, We used AlertDialog with Cancel and Okay button option. The user can do click either okay or cancel button. Here also we used callback while clicking on any button to perform the action that notifies to update accordingly. Here I am creating an interface DialogListener for using the callback.

See this is pretty easy to understand, Here we do not require any interface or instance of an interface. It works automatically all subscribed observers will get notify. This is the one case that we can prefer to use RxJava.Now Let's take another example to encourage us to use RxJava in building the awesome app. We are using multiple background task in our app to fetch the data from server and display on UI and much more kind of thing we do. To achieve this background task mostly we used async task. But Async task has its own drawback like When your application running multiple async tasks then we do not know which task will be finished first and all.Async task is running in activity context and it is partially run by background thread and partially run by Main Thread. In case if Android Os will kill your application because of memory not available at that time then Your activity will be destroyed and then the context of activity will be destroyed. But the async task will be running because its half part will be in the background thread. its cause memory leak. Sometimes error handling in Async task is also very dirty.

If you are dealing with Activity and fragment lifecycle by using rxJava then please unsubscribe the Observable emitting to avoid the memory leak.In Rx java, it is the very stable way we can do or we can switch threads to tell them which thread will be good suite to execute my task. For multiple tasks, you can easily merge your task and cache. This is not the only thing you can do much more with RxJava.

So now we can get that why should be used RxJava in an Android. this is few things I have pointed but it provides much more and many ways we can use Rxjava in our application to make the awesome app. In my next tutorial, I will focus some more good features of RxJava that we must to used to build the awesome and stable app. Thanks for reading this post.