Photo by Jeremy Bishop on Unsplash

šŸŒŠ StateFlow, End of LiveData?

In this article, weā€™ll learn how to use Kotlin Coroutine StateFlow in Android instead of LiveData.

Shreyas Patil
ScaleReal
Published in
3 min readMay 22, 2020

--

In the recent release of Kotlin coroutines library (1.3.6), you can see there a new class ā€” StateFlow. So whatā€™s this and how it works? Letā€™s seeā€¦

What is StateFlow ?šŸ¤·ā€ā™‚ļø

  • Itā€™s basically a new primitive for state handling.
  • Itā€™s designed to eventually replace ConflatedBroadcastChannel for state publication scenarios.
  • It is a flow that emits updates to its collectors.
  • Value can be observed by collecting values from the flowšŸŒŠ.

Still not getting? Hereā€™s a quick demo to understand ā€”

Demonstration of using StateFlow. Run this code to see the output.

I think now you get it whatā€™s exactly ā€” StateFlowšŸ˜ƒ. So whatā€™s happening here is whenever weā€™re updating the value of stateFlow then it emits value to its collectors.

To manage state in Android we generally used Android Arch. componentā€™s LiveData which is lifecycle-aware. We can replace it with StateFlow. Letā€™s see how to use it with Android. Letā€™s write some code!

āš”ļø Getting Started

Open Android Studio and create a new project. Alternatively, you can simply clone this repository. This is a very simple counter app for demonstrating the use of Kotlin Coroutineā€™s StateFlow API.

Weā€™ll be using MainViewModel to manage our data of MainActivity.

Now you can compare its implementation using LiveData.

  • MutableStateFlow has a setter property for value.
  • Weā€™ve declared an instance of StateFlow i.e. countState which weā€™re exposing for activity (Itā€™s a read-only field).
  • StateFlow has a property called value by which you can be safely read at any time.

Now letā€™s implement our MainActivity ā€”

Here, weā€™ve initialized ViewModel for activity. Now letā€™s implement the initView() method which will initialize our Counter App UI.

Everything looks cool now! šŸ˜ƒ. Letā€™s observe for count value now to keep track of counting and show it on UI accordingly.

Hereā€™s we have collector which will be executed whenever the value of a countState is updated. We also made it lifecycle-aware as weā€™ve used it under lifecycleScope. It looks simple, right? Thatā€™s it! šŸ˜Ž

Now letā€™s run this app and see if itā€™s working.

Counter app demo

Ainā€™t it Sweettttt šŸ˜.

We can implement the same using LiveData too. Whatā€™s different then? šŸ¤·ā€ā™‚ļø

We can use powerful flow operators with StateFlow like combine, zip, etc which can give us more great experience than LiveData. Yes, thatā€™s it.

Final Words:

  • StateFlow is really easy to handle and implement.
  • Currently, it doesnā€™t support LiveDataā€™s onActive() or onInactive() like callbacks but this will be possible once SharedFlow is officially released šŸš€.
  • Its behaviour is the same as LiveData along with more operators and great performance šŸ˜Ž. Then we should consider using it instead of LiveData.

At Scalereal We believe in Sharing and Open Source.

So, If you found this helpful please give some claps šŸ‘ and share it with everyone.

Sharing is Caring!

Thank You :) šŸ™

If you need any help, you can contact me here.

Resources:

--

--

Shreyas Patil
ScaleReal

Google Developer Expert for Android, šŸ‘Øā€šŸ’»Engineer @ Paytm ā¤ļø Android & Kotlin | More info: https://shreyaspatil.dev