A Short Note – How Does The MVI Work In Android ?

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (How does the MVI work in Android?).

In this note series, We will understand what MVI architectural pattern is, how it resolves these challenges in android.

Android architectural patterns are growing day by day. As we develop apps, we face 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.

So Let’s begin.


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.

Let’s see what is the role of each component of MVI.

  • 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. We can say we will send models as inputs to the Intents, which can load it through Views.


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 and Disadvantages of MVI

Let’s see what are the advantages and disadvantages of MVI.


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 than 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 challenge 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 it is the state of the intent. In terms of usability, this has to be handled.


Conclusion

In this note series, we understood about what MVI architectural pattern is, how it resolves these challenges in android.

Thanks for reading! I hope you enjoyed and learned about MVI Concept in Android. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow official website and tutorials of Android as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

Exit mobile version