Presenting data with MVVM
In this section, we will analyze the Model-View-ViewModel architecture pattern and how it is implemented for Android applications.
MVVM represents a different approach to the Humble Object pattern, which attempts to extract the logic out of activities and fragments. In MVVM, the View is represented by activities and fragments as it was in MVP, the Model plays the same role, managing the data, and the ViewModel sits between the two by requesting the data from the Model when the View requires it. The relationship between the three is as follows:
In Figure 8.3, we see a unidirectional relationship between the three components. The View has a dependency on the ViewModel, and the ViewModel has a dependency on the Model. This allows for more flexibility because multiple Views can use the same ViewModel. For the data to be updated in the View, MVVM requires an implementation of the Observer pattern...