StateFlow with Jetpack Compose
Recently I have heard that StateFlow will replace LiveData. StateFlow and LiveData have similarities and both are observable. Let’s know about it more.
What is StateFlow
According to official documentation
StateFlow is a state-holder observable flow that emits the current and new state updates to its collectors. The current state value can also be read through its value property. To update state and send it to the flow, assign a new value to the value property of the MutableStateFlow class.
We will learn it with a basic example.
What are we Reading
It’s a great app. Isn’t it? 😉
First, create a ViewModel.
MutableStateFlow is a StateFlow whose value can be changed, and it can be used bidirectionally. When we call
titleInputFlow will change and every receiver of
titleInputFlow will get the latest data.
This is how we will use StateFlow in Jetpack Compose. We will collect the StateFlows as a state by
collectAsState() . So now we can still get the data if the app goes to onResume() or onPause() state.
This is the basic design. We are passing here the title, change and click listener.
I know this is a very basic example. There are many other uses of StateFlow . Please check the official documentation to know more.
That's all for today. If you are interested in Clean architecture in Android please check the below article.
Clean architecture in Android (Jetpack Compose, Kotlin, MVVM) ーPart- 1
Making an android application is kind of easy( Compared with creating with Clean architecture . Please feel free to…
Thanks for staying till the last. See you again 😃.