Migrate from LiveData to Kotlin Flow

Mayur Waghmare
Mobile Innovation Network
3 min readJun 13, 2024

LiveData is one of the most popular components of the Android Jetpack family. Right now, many modern Android applications use LiveData in the “UI” and sometimes in “Data” layers.

Sadly, there are certain drawbacks to this technique, which we shall go over in this article.

Problems with LiveData

  • Readable and Writable only on the main thread
  • It updates its value on the main thread, even when set asynchronously using postValue().
  • No Direct support of error handling — you might need to use additional mechanisms to propagate errors.
  • Limited built-in transformation capabilities compared to Flows.

Advantages of Flow

  • Flows are not tied to the Android lifecycle and can be used in various contexts.
  • Has better support for error handling as it uses Kotlin’s Result type/custom error handling mechanisms.
  • It has powerful operators for transforming and processing data in a reactive manner, similar to RxJava.
  • As part of Kotlin coroutines, it benefit from coroutine’s cancellation and structured concurrency features.

Replacing LiveData with Flow

We have the two most popular scenarios of using LiveData object in ViewModel classes:

- observing emitted value or latest available value (LiveData)
-
observing value only once (SingleLiveEvent)

Let’s take an example of “Task Manager” application, which has the following cases :

  • Displaying all tasks
  • Emitting an action when we want to navigate to the task details screen

Now let’s see,
How we can replace LiveData with StateFlow & SharedFlow?

StateFlow —Collect the latest emitted data

We can replace LiveData<T> with StateFlow<T> when we want to be notified when the object is changed and receive the last emitted value

In above code after changing screen rotation the last available value of UiState.Loading will be emitted

SharedFlow — Collect emitted data once

When we want to collect data only once, the SharedFlow is a good choice.

When screen rotation happens, the value won’t be collected one more time because of a combination of the SharedFlow type and replay = 0 parameter.

Note: Use replay parameter, which determine how many times the values can be collected.

Should we switch from Live data to Flow?

It depends on what you want,

  • If you want a manual, full and versatile control over the app and also take advantage of Kotlin coroutines, go for State Flow.
  • If you want a partially automatic or relatively easy-to-use method for your app , I will say — stick with Live Data.

Thank you for taking the time to read this article. If you found the information valuable, please consider giving it a clap or sharing it with others who might benefit from it.

Any Suggestions are welcome. If you need any help or have questions for Code Contact US. You can follow us on LinkedIn for more updates 🔔

--

--

Mayur Waghmare
Mobile Innovation Network

"Mobile App Developer specializing in Android, iOS, and Flutter. Passionate about crafting user-focused, efficient mobile solutions."