Kotlin Flows on Android

Şeyma Nur Fırat
4 min readJul 5, 2022

--

Hello everyone🧚🏼‍♀️ What is flow? What is difference with livedata? Let’s start🌟

Firstly, what is observer design pattern? Observer design pattern is
a behavioral design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing.

LiveData

LiveData is an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services. This awareness ensures LiveData only updates app component observers that are in an active lifecycle state.

👉🏻Livedata provides communication with UI and Viewmodel. And it observing changes.

👉🏻Livedata object generally create in viewmodel.

Using LiveData provides the following advantages:

  • No memory leaks
  • No crashes due to stopped activities
  • Always up to date data
  • Proper configuration changes
  • Sharing resources
  • No more manual lifecycle handling

Creating Live data:

Flow

In coroutines, a flow is a type that can emit multiple values sequentially, as opposed to suspend functions that return only a single value.

👉🏻It’s all about being notified about changes in your code and sending them through a pipeline that potentially modifies them.

👉🏻Flow is a coroutine that can emit multiple values over a period of time.

👉🏻Flow doesn’t hold a value.

👉🏻Flow is not life-cycle aware.

👉🏻Flow should use in Viewmodel class.

👉🏻Flow is a cold flow.

Cold Flow: Doesn’t emit values, if there is no collector.

Hot Flow: Emit values if there is no collector.

Creating Flows:

We are creating countdown timer for understanding flows.

emit(): Function is for emitting the number. Basically, counts down by one second and notify ui about that change.

delay(): Add 1 second wait.

It will emit values from 10 to 1 with 1000ms delay. emit() function is for emitting the number.

collectAsState(): Use CollectAsState keyword for converting value to state. As the state changes, text will change because of the recomposition.

Then, we showed countdown timer on ui:

Output:

StateFlow

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.

👉🏻Stateflow need an initial value.

👉🏻Stateflow is used to handle UI state.

👉🏻Stateflow doesn’t emit same value again.

👉🏻We keep the states and their values in stateflow.

👉🏻Stateflow is used to keep state, just like livedata but without lifecycle awareness.

👉🏻Use repeatOnLifecycle to make it lifecycle aware. This is using for safely collecting flows in UI layer.

repeatOnLifecycle prevents you from wasting resources, as it restarts and stops the flow collection on the targeted state of lifecycle change.

Creating State Flow:

SharedFlow

The shareIn function returns a SharedFlow, a hot flow that emits values to all consumers that collect from it. A SharedFlow is a highly-configurable generalization of StateFlow.

👉🏻SharedFlow used to send one time events. For instance, show snackbar, navigate to different screens etc.

👉🏻With SharedFLow an event will be lost if the consumer is not ready to receive events.

👉🏻SharedFlow doesn’t need an initial value.

👉🏻If there are multiple collectors listening to changes, use sharedFlow.

👉🏻SharedFlow is used to handle UI events.

Creating Shared Flow:

Now, we are doing simple project for comparison.

Firstly, we create live data, shared flow and state flow object in MyViewModel.kt class. Then, create function for changing values.

Now, we create three buttons for changing value in MainActivity.kt class.

In MainActivity.kt class, we should call ComparisonScreen function.

Output:

Shared flow doesn’t have initial value, so when we clicked the button, emit the value.

--

--

No responses yet