MVVM on Android with the Architecture Components + Koin

Please follow and like us:

After having defined the model and all its different parts, it is high time to implement the view-model, for that we will use a class that came with Android Jetpack the ViewModel class.

The class is designed to store and manage UI-related data in a lifecycle conscious way. The class allows data to survive configuration changes such as screen rotations.

As you can notice, the view-model takes as parameter the repository, this class knows all the data sources of our application, in the init block of the view-model we refresh the data of the database by calling the refresh method of the repository, but the view-model also has a data property that directly retrieves the data locally, this guarantees that the user will have on his interface some data even if the device is not connected.

Note: I use a helper class that allow me to manage the state of the loading

The view is the last component of the architecture, this component will communicate directly with the view-model to retrieve the data and populate a recycler-view for example, in our case the view is just a simple activity.

The view only observes the change of the data to automatically update the data at the interface level, in our case the view also observes the state of loading operations in the background using the loadingState property previously defined in the view-model.

As you can see, I have retrieved an instance of the view-model by using inject function, we will see how it’s work in the next part

If you are vigilant you will notice that until then I have not yet created a repository object let alone the parameters that the repository takes, this is precisely what we will do by using dependency injection, for that we will use a library that I personally appreciate Koin.

By using Koin, we will create the important objects that our application will have to use at the same place and we will only have to call its objects at different places in our application using the magic of Koin.

If you wanted to learn how to configure and use Koin in an android application I advise you to read my post Dependency Injection with Koin


The Module.kt contain the declaration of object that the application is going to use, in the view, we are using inject that tell to Koin that we need a view-model object, Koin will try to found this object in the module previously defined and will assign the property userViewModel with this instance, if there isn’t the corresponding object in the module Koin will throw an exception.

In our case, the code will compile properly because we have created an instance of the view-model in the module with the appropriate parameter

The same scenario will be applied to the repository inside the view-model, this instance will be retrieved from the Koin module because we already created an instance of the repository with the appropriate parameter inside the Koin module.

You want to learn more about dependency injection with koin see Dependency Injection with Koin

The biggest job of a software engineer is not the development but the maintenance, the more the code is based on a good architecture the easier the maintenance and the testability of the application will be, that’s why it’s often important to use patterns to avoid creating a bomb instead of software.

You can find the complete code of the application on my GitHub with this link

Le pattern Modèle – Vue – Vue Modèle (MVVM)

Je travaille actuellement sur le développement d’une application Windows 8 (C# et XAML) en utilisant une architecture…


You can’t perform that action at this time. You signed in with another tab or window. You signed out in another tab or…

What About Today News brings you Latest News around the World. You may Like and Follow our Facebook and Twitter and feel free to Contact Us at any time.

Please follow and like us:

Related Post

Add a Comment

Interesting? Please spread the post :) Thank you very much!