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
May 22, 2020 · 3 min read

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…

  • 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🌊.
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 use powerful flow operators with StateFlow like combine, zip, etc which can give us more great experience than LiveData. Yes, that’s it.

  • 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:

ScaleReal

We build applications that scale beyond reality

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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