MVVP for Android development

In this article, our primary focus will be specifically on the Android Application Architecture and on finding out the main differences between the popular architecture of apps on Android MVVM vs MVP.

The Android architecture components make their product more scalable and easier to maintain effectively. The developers primarily use architectures like MVP and MVVM in Android to organize the apps into more logical components.

MVVM and MVP are without a doubt the most adopted architectures for Android application development, whereas the MVC (Model View Controller) is the one that used to be a great option in the past few years.

What is Android Architecture?

Android architecture is the way you structure your Android project code so that your code is scalable and easy to maintain. Developers spend more time maintaining a project than initially building it, so it makes sense to follow a proper architectural pattern.

Find your next developer

Aloita tästä

Differences between MVVM and MVC

In Android, MVC refers to the default pattern where an Activity acts as a controller and XML files are views. MVVM treats both Activity, Fragment and XML as views, the ViewModel classes are where you write your business logic. It completely separates an app's UI from its logic.

As the image shows, the different components of View - Model - ViewModel with what each one contains.

View Viewmodel and Model explained

Pros of the MVVM pattern

  • Review the complete setup at the compilation stage, resulting in reducing extra efforts to the minimum.

  • Allow for using the official library of Google for a strong generation of components and other elements.

  • Code similarity and simplicity.

  • Using binding adapters allowing the user for writing custom XML attributes.

Cons of the MVVM pattern

  • Testing difficulty, MVVM makes writing Unit test harder.

  • Code confuses the developer because XML in some cases can be seen in the XML.

  • User must use one of two methods: Data binding or any other view method, when working with view.

Pros and cons of MVVM

MVVM best practices

With the MVVM pattern, developers can build robust, high-quality apps.

The most important principle to follow in MVVM is the separation of concerns. It's a common mistake to write all your code in an Activity or a Fragment. These UI-based classes should only contain logic that handles UI and operating system interactions. By keeping these classes as lean as possible, you can avoid many problems related to the component lifecycle, and improve the testability of these classes.

Following are the best practices for building apps using MVVM:

Don't store data in the app components

Avoid designating your app's entry points, such as activities, services, and broadcast receivers as sources of data.

Instead, they should only coordinate with other components to retrieve the subset of data that is relevant to that entry point. Each app component is rather short-lived, depending on the user's interaction with their device and the overall current health of the system.

Reduce dependencies on Android classes

Your app components should be the only classes that rely on Android frameworks SDK APIs such as Context, or Toast. Abstracting other classes in your app away from them helps with testability and reduces coupling within your app.

Create well-defined boundaries of responsibility between various modules in your app

For example, don't spread the code that loads data from the network across multiple classes or packages in your codebase. Similarly, don't define multiple unrelated responsibilities—such as data caching and data binding – in the same class. Try to get all the ins and outs of the app architecture to further advance with this.

Expose as little as possible from each module

For example, don't be tempted to create a shortcut that exposes an internal implementation detail from a module. You might gain a bit of time in the short term, but you are then likely to incur technical debt many times over as your codebase evolves.

Focus on the unique core of your app so it stands out from the crowd

Don't reinvent the wheel by writing the same boilerplate code again and again. Instead, focus your time and energy on what makes your app unique, and let the Jetpack libraries and other recommended libraries handle the repetitive boilerplate.

Consider how to make each part of your app testable in isolation

For example, having a well-defined API for fetching data from the network makes it easier to test the module that persists that data in a local database. If instead, you mix the logic from these two modules in one place or distribute your networking code across your entire codebase, it becomes much more difficult, if not impossible, to test effectively.

Persist as much relevant and fresh data as possible

That way, users can enjoy your app's functionality even when their device is in offline mode. Remember that not all of your users enjoy constant, high-speed connectivity, and even if they do, they can get bad reception in crowded places.

MVP vs MVVM comparison

Conclusion

MVVM is one of the most famous architecture patterns used for Android development. It has its cons and pros, moreover, this article describes why using it is more efficient and usable over the traditional MVC and MVP design patterns. Most importantly, it makes developers build an app that is more robust, high quality, decopuled, testable and with a more maintainable code.

Khalid Kadamani

Khalid Kadamani

Senior Android Developer, PHP | Laravel Developer | Business Analysis

Löydä seuraava kehittäjäsi päivien, ei kuukausien sisällä

Kun otat yhteyttä, järjestämme lyhyen 25 minuuttia kestävän tapaamisen, jonka aikana:

  • Kartoitamme yrityksenne kehitystarvetta
  • Kertoa prosessimme, jolla löydämme teille pätevän, ennakkotarkastetun kehittäjän verkostostamme
  • Käymme läpi askeleet, joilla oikea ehdokas pääsee aloittamaan – useimmiten viikon sisällä

Keskustele kanssamme