Photo by Pablo García Saldaña on Unsplash

Replacing LiveData with StateFlow

Introduction

LiveData

StateFlow

Code

Using LiveData

ViewModel managing LiveData
Observing LiveData at MainFragment

Using StateFlow

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
ViewModel managing StateFlow
Observing StateFlow at MainFragment

Additional information

import kotlinx.coroutines.flow.collect

Further reading

Android Developer @iFood

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store