StateFlow vs. Flow vs. SharedFlow vs. LiveData... When to Use What?! - Android Studio Tutorial

Philipp Lackner · 177K views · 2021 · 18:08

Open on YouTube

Software practice, open source & careersSoftware architecture & designFunctional programmingtutorial

Why Stack Overflow recommends it

Stack Overflow users link it when explaining how StateFlow, SharedFlow and LiveData differ, and as the source of sample code they ask follow-up questions about.

Recommended by 2 people across 2 different months, 2022 to 2022.

What it is

A roughly 18-minute Android Studio tutorial by Philipp Lackner comparing LiveData, StateFlow, Flow and SharedFlow in Kotlin. It walks through each observable type with code and explains when each fits, including UI state versus one-time events.

What people said

Recommends Sam Chen · 2022-02 · Stack Overflow · on “Use of SharedFlow in Android kotlin”

MutableStateFlow is similar to MutableLiveData , both have .value to access the value or to change the value. MutableSharedFlow is similar to SingleLiveEvent , used for observing instant-state changes. More example and tutorial you can check out this video: [this video]

Recommends Javad Shirkhani · 2022-05 · Stack Overflow · on “Android LiveData advantage than other observable libraries”

Based on philip lackner [this video] : If you need to store a value that can change over time(state), use state flow or live data , otherwise use shared flow or Channel If you like Coroutines use State Flow, Live data use main thread. Live data is lifecycle-aware while state flow is not lifecycle-aware by default but can be using lifecycleScope + repeatOnLifeCycle(). Both of them use in ViewModel to store UI state. If you have multiple observers, use shared flow instead of channel. Both of them use to send one time events(like when the user successfully connected). Shared flow use coroutines.

Cites HelloCW · 2022-02 · Stack Overflow · on “Why do the author need to use asStateFlow() in Compose?”

The Code A is a sample code from the video [this video] . I can't understand why the author need to use asStateFlow() . I think the Code B is OK, right? Code A class MainViewModel: ViewModel() { private val _stateFlow= MutableStateFlow("Hello World") val stateFlow = _stateFlow.asStateFlow() ... } Code B class MainViewModel: ViewModel() { private val _stateFlow= MutableStateFlow("Hello World") val stateFlow = _stateFlow ... }

Cites Eldhopj · 2022-03 · Stack Overflow · on “How to use Single Live Event to show toast in Kotlin”

The answer is correct and verified, MutableSharedFlow is suable for single event types please refer: [this video]

Cites Ishika Singh · 2022-09 · Stack Overflow · on “Why does Flow (kotlinx.coroutines.flow) not working with Retry even though I manually set as null in Android?”

So basically, on the snackbar action button, I want to Retry API call if user click on Retry. I have used core MVVM architecture with Flow. I even used Flow between Viewmodel and view as well. Please note that I was already using livedata between view and ViewModel, but now the requirement has been changed and I have to use Flow only. Also I'm not using and shared or state flow, that is not required. Code: Fragment: private fun apiCall() { viewModel.fetchUserReviewData() } private fun setObservers() { lifecycleScope.launch { viewModel.userReviewData?.collect { LogUtils.d("Hello it: " + it.code) setLoadingState(it.state) when (it.status) { Resource.Status.ERROR -> showErrorSnackBarLayout(-1,