Android MVI Architecture

Soundhar P
5 min readSep 9, 2021

--

Hi welcome you all… In this blog, we are going to discuss about Android MVI Design pattern. Now a days Android architectural patterns are evolving day by day. As we develop apps, we start facing new challenges and issues. New patterns will be discovered as we keep solving similar challenges.

As Android Developers, we have MVC, MVP, and MVVM as the most commonly used patterns. All of them use an imperative programming approach. With this approach even though most of our challenges will be resolved, we still face some challenges regarding the thread safety, maintaining states of the application. With this let’s see what MVI architectural pattern is, how it resolves these challenges, and how to get started on MVI.

Here, I will walk you through,

  • Overview of Android Design Architecture.
  • List of Existing Design patterns.
  • MVI Architecture.
  • Why MVI ?
  • Key Strength of MVI.
  • Architecture Layer.
  • How does the MVI works.
  • And Major Advantages & Disadvantages of MVI concept

Android Design architecture.

The Android framework provides a lot of flexibility in deciding how to organize and architect an Android app.

While this freedom is very valuable, it can also lead to apps with large classes, inconsistent naming schemes, as well as mismatching or missing architectures.

These types of issues can make testing, maintaining and extending apps more difficult. So, most of the people moved away from the traditional Model View Controller (MVC) pattern to either Model View Presenter (MVP) or Model View Viewmodel (MVVM) pattern.

Existing Design Patterns.

These are all the existing design patters which helps us to develop Android applications currently.

What is MVI architecture?

MVI stands for Model-View-Intent. This pattern has been introduced recently in Android. It works based on the principle of unidirectional and cylindrical flow inspired by the Cycle.js framework.

Model: Unlike other patterns, In MVI Model represents the state of the UI. i.e for example UI might have different states like Data Loading, Loaded, Change in UI with user Actions, Errors, User current screen position states. Each state is stored as similar to the object in the model.

View: The View in the MVI is our Interfaces which can be implemented in Activities and fragments. It means to have a container which can accept the different model states and display it as a UI. They use observable intents(Note: This doesn’t represent the Android traditional Intents) to respond to user actions.

Intent: Even though this is not an Intent as termed by Android from before. The result of the user actions is passed as an input value to Intents. In turn, we can say we will be sending models as inputs to the Intents which can load it through Views.

Data Flow Diagram

Here you can understand the Data flow of MVI design patterns with the helps of this pictorial reference.

User interaction will always connecting with the View. But the data will be loaded through the Intent by sending the input value as User action which directly meant the Model as we discussed above.

Why MVI?

You may think about the Usage of this new Patterns, since we already using more than one architecture in android development.

As the application grows or unplanned in advance functionality is added — without clear state management, the view rendering along with the business logic can get a little bit messy.

The more scalable the application code is, the more flexible it is to new ideas and updates. Scalability, flexibility and easy testability — that’s what the MVI architecture offers us.

Key Strength of MVI:

  • Unidirectional and cyclic data flow
  • Easiness of catching and fixing bugs
  • Easiness of code testability
  • Ability to test all layers of the application with unit tests

How does the MVI work?

User does an action which will be an Intent → Intent is a state which is an input to model → Model stores state and send the requested state to the View → View Loads the state from Model → Displays to the user.

If we observe, the data will always flow from the user and end with the user through intent. It cannot be the other way,

Hence its called Unidirectional architecture. If the user does one more action the same cycle is repeated, hence it is Cyclic.

Advantages of MVI

  • Maintaining state is no more a challenge with this architecture, As it focuses mainly on states.
  • As it is unidirectional, Data flow can be tracked and predicted easily.
  • It ensures thread safety as the state objects are immutable.
  • Easy to debug, As we know the state of the object when the error occurred.
  • It’s more decoupled as each component fulfills its own responsibility.
  • Testing the app also will be easier as we can map the business logic for each state.

Disadvantages of MVI

  • It leads to lots of boilerplate code as we have to maintain a state for each user action.
  • As we know it has to create lots of objects for all the states. This makes it too costly for app memory management.
  • Handling alert states might be challenging while we handle configuration changes. For example, if there is no internet we will show the snackbar, On configuration change, it shows the snackbar again as its the state of the intent. In terms of usability, this has to be handled.

Project Source Code.

For the project source code, check here.

As we have done some simplifications in this project for the Beginners level, so, we can improve this project to go to the Advanced level.

I hope you have learned the MVI architecture in Android.

Keep Learning, Keep Exploring, Keep Growing

Thank you…!

--

--