Redux Style State Management for Android Apps | by Tanya Masvita | Feb, 2022

State Management is a way to facilitate communication and data transfer between components by centralizing all the state of various UI controls to handle data flow across the application. It is one possible manifestation of the single source of truth paradigm, that is, the state is stored in a single object and that single object serves as the only true source of state for our application.

State Management is not independent of app architecture. As a matter of fact, State Management is because of app architecture. According to the SOLID principles an exceptional architecture is one within which modules are closed for modification and open for extension. That means when a new feature is to be added to an app, modules should not be modified to accommodate the new feature, but rather code should be added to the modules that implement the new feature, and the added code should preserve the invariant that modules are closed for modification and open for extension.

Again assuming independence from all other features, here are 4 main components required to make this work.

First, define all actions that can be made from within the Profile feature. The actions will depend on the type of feature you are developing. For this example we might have something like this:

This is the object holding all states used by the user profile feature. It is immutable thus changes to any of the data it holds it will force the creation of a new instance of the view state containing the modified data. Here’s one way to implement it.

The ViewModel exposes only state to the view (the Profile screen). The implementation of the ProfileViewModel is fairly complex so let’s start with the implementation followed by a brief explanation.

This is the main UI component of the profile feature implemented using Jetpack compose.

Leave a Comment