Sharing data between Views and composable functions
State is app data that may change over time. Recomposition occurs when state being used by a composable changes. To achieve something similar in the traditional View world, we need to store data in a way that changes to it can be observed. There are many implementations of the Observable pattern. The Android Architecture Components (and subsequent Jetpack versions) include
MutableLiveData. Both are frequently used inside ViewModels to store state outside activities.
I introduced you to ViewModels in the Surviving configuration changes section of Chapter 5, Managing the State of Your Composable Functions, and the Persisting and retrieving state section of Chapter 7, Tips, Tricks, and Best Practices. Before we look at how to use ViewModels to synchronize data between Views and composable functions, let's briefly recap on key techniques, as follows:
- To create or get an instance of...