Android MVVM architectural design pattern with RxJava in Kotlin

To build an awesome android application, Architectural design pattern plays an important role. It makes our code modular and communication with different components becomes easy. By using Architectural design we can build an application which can be testable and maintainable easy in the future or whenever we want to add a new feature or remove a feature, it will not cause any complication.

There are many design pattern available to build awesome apps like MVP (Model View Presenter), MVVM (Model View View Model) and Android  Architectural design Component recently given by Google. To picking any design pattern is the little bit difficult to decide. Its all about depend on Android Developers that which design pattern they like most. Every design pattern works nicely with all component.

In this tutorial, we will discuss the MVVM Android Architectural design to use to build the awesome android application.

Model: Model represents the classes that used for creating the business model of an application. It means how the data can manipulate.

View: View represents the user interface means Button or TextView. View displays the data from the model.The view which binds with Observable variables and action triggered for ViewModel.

ViewModel: ViewModel is a middle layer for interaction between the View and model. Its responsibility is preparing the Observable data which is needed to view and also hooks for model event triggered from view.
This pattern is using the data binding concept which means that multiple views contain with single ViewModel. ViewModel should have all information which is required for the view.  In this case, ViewModel that can be reused for multiple views. For Single ViewModel interaction for a module, our debug and unit testing becomes easy. A developer can find the issue easily and it makes easy to add any new feature to existing code. Overall it makes our life pretty easy & simple.

Your data update or change should be aware of lifecycle otherwise it will memory leak. If you are using the RxJava Observable sequence then make sure that it uses correct way because RxJava is not aware of Lifecycle. Always try to use LiveData and Databinding ObservaleField for the better result because they are aware of lifecycle event and avoid the memory leaks. Here You can learn  Why is necessary to take care android memory leaks?

To understand the reactive Rx I would be recommended to check these posts of understanding of Java 8 stream and Rx Observables, and basic understanding and practice features and functions of RxJava, and understanding practice RxJava with RxBinding in Android.

Let’s take an example of MVVM with Databinding ObservaleField and RxJava to send the tweet to our timeLines.

I have created the TimeLineFragmentViewModel is a ViewModel to handle all the business logic will be here. I used the Twitter API call to send a tweet to Twitter and get the result of the tweet from Twitter API client. Here callback result is subscribed to the publisher by using the RxJava.  Now, let’s check how is this ViewModel is communicating with View?

We can use DataLayer in XML to bind the model directly.

Here Data change or update will observe by Observable to update the view by using RxAndroid Scheduler MainThread. It can be subscribed with success or failure the causing of exception. Now suppose in future if any new requirement comes, for example, we will support offline then here we need to change few things in ViewModel, others remain same especially View part. It means it will not create so compilation to update or add any new feature because our code structure is modular that makes the Unit Test easy. Here you can learn basics of Kotlin.

If You are wondering the MVP design pattern with RxJava then I would be recommended to check this post to get help MVP Acrcutureal design pattern with RxJava. Here are the best offer and recommendation for you to download this. Here you can get the full source code from Github of MVVM with Kotlin.

You should always take priority while developing an android application in term of performance, write less code by using best data structure and application should be used less memory. Here you can learn about the Android application performance and stability is really mattered?

Please do subscribe email to get every newsletter on this blog and if you feel that this post will help you to better understand then do not forget to subscribe, share and comment below.

Happy coding 🙂

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

Leave a Reply

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

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