Read More

Android MVVM architecture

Every developer wants to create a clean architecture of project and also achieves this on the first release of the application. But for big projects, if you want to fix any bug, you have to put lot of consideration into the structure of the application code becomes of the complexity of code. The solution of this problem is Android MVVM architecture.

MVVM also called Model-View-ViewModel is project architecture which is currently in fashion. The reason behind its popularity is its clear architecture.

MVVM – Clean Architecture

Android MVVM architecture is used to separate our view (i.e. Activity and Fragments) from your business logic. When we use MVC & MVP architecture, it is very hard to manage the code.

Model-View-ViewModel with Clean Architecture is pretty good in such cases. It goes one step further in separating the responsibilities of your code base. It is used to abstract the logic of the actions that can be performed in your app.

Model

The model is used to represent the actual data and/or information with which we are dealing.

Example -> Model might be user login data (Email, Password)

View

View is the presentation of the data.

Example -> xml and activity files in our Android project.

Model-View-ViewModel

It acts as the link/connection between the Model and View and makes stuff look pretty.

Lets take an example ->

User login-in through MVVM architecture ->

Firstly create a model class which represents our data.

Here, we are binding email & password variable with our xml through “@Bindable” & “BR.email & BR.password”. Now, when user enter email & password, it will automatically saved in Model class variables.

There are one edit text for email, one edit text for password and a submit button.

Now, we are going to create our ViewModel class.

Create a viewModel class which works as connection between our view & model class. Here, we create our Model class object in his “init” method and a method callApiMethod() which will be called when we click on “Submit” button.

Now, we are going to create Activity and connect our view & viewModel in it.

Here, we observer our mutableLiveData. When we update responseCode value, it will be observed in our activity.

For more information, you can check this link -> MVVM

 

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close