Member-only story
Android Data Binding With MVVM: Using StateFlow and ViewModel
A guide to building MVVM architecture-based apps with Data Binding
This is the third part of a series in which you’ll learn about Data Binding basics and how to use Data Binding with architecture components like LiveData
and ViewModel
. Today, you’ll learn how to use Kotlin coroutines and StateFlow
to update the UI via Data Binding from a ViewModel
.
- “Android Data Binding with MVVM Architecture (Part 1)”: Basics > Two Way Binding > Code Optimization
- “Android Data Binding with MVVM Architecture (Part 2)”: Data binding with LiveData and ViewModel
- “Android Data Binding with MVVM Architecture (Part 3)”: You’re here.
Introduction
StateFlow
The Flow API in coroutines is a better way to handle a stream of data. StateFlow
attempts to bring the powerful features of Flow to manage a state in your Android application.
As explained on GitHub, StateFlow
comes in two variants: StateFlow
and MutabaleStateFlow
.
“The
StateFlow<T>
interface is a read-only view that gives access to the currentvalue
and implements aFlow<T>
to collect updates to the values.The…