0% found this document useful (0 votes)
12 views4 pages

CallbackFlow - Callback To Flow API in Kotlin

This blog post by Amit Shekhar explains how to convert a Callback to Flow API in Kotlin using the callbackFlow function. It provides an example with LocationManager to demonstrate the conversion process, detailing steps like creating a function that returns Flow, using trySend for updates, and unregistering with awaitClose. The article is part of a series on Kotlin Flow API, aimed at helping developers integrate modern practices into their Android projects.

Uploaded by

josehieegamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

CallbackFlow - Callback To Flow API in Kotlin

This blog post by Amit Shekhar explains how to convert a Callback to Flow API in Kotlin using the callbackFlow function. It provides an example with LocationManager to demonstrate the conversion process, detailing steps like creating a function that returns Flow, using trySend for updates, and unregistering with awaitClose. The article is part of a series on Kotlin Flow API, aimed at helping developers integrate modern practices into their Android projects.

Uploaded by

josehieegamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

6/24/25, 4:26 PM callbackFlow - Callback to Flow API in Kotlin

Outcome Twitter YouTube LinkedIn GitHub Blog


School
callbackFlow - Callback to Flow API
in Kotlin
Amit Shekhar
January 31, 2023

I am Amit Shekhar, Co-Founder @ Outcome School, I have taught and mentored many
developers, and their efforts landed them high-paying tech jobs, helped many tech
companies in solving their unique problems, and created many open-source libraries
being used by top companies. I am passionate about sharing knowledge through open-
source, blogs, and videos.
Join Outcome School and get high paying tech job: Outcome School
Before we begin, we’d like to mention that we’ve launched our YouTube channel.
Subscribe to the Outcome School YouTube Channel.
https://outcomeschool.com/blog/callback-to-flow-api-in-kotlin 1/5
6/24/25, 4:26 PM callbackFlow - Callback to Flow API in Kotlin

In this blog, we will learn how to convert any Callback to Flow API in Kotlin using
callbackFlow.
This blog is a part of the series I have written on Flow API in Kotlin:
Mastering Flow API in Kotlin
Creating Flow Using Flow Builder in Kotlin
Terminal Operators in Kotlin Flow
Cold Flow vs Hot Flow
StateFlow and SharedFlow
Long-running tasks in parallel with Kotlin Flow
Retry Operator in Kotlin Flow
Retrofit with Kotlin Flow
Room Database with Kotlin Flow
Kotlin Flow Zip Operator for Parallel Multiple Network Calls
Instant Search Using Kotlin Flow Operators
Exception Handling in Kotlin Flow
callbackFlow - Callback to Flow API in Kotlin - YOU ARE HERE
Unit Testing ViewModel with Kotlin Flow and StateFlow
We use many libraries in our Android Project that provides the callback way to use
instead of the Flow API way. As nowadays, we all have started using Kotlin Coroutines
Flow API in our projects, so it becomes our responsibility to implement things in a way
that supports Flow API.
So, we need to learn how to convert any Callback to Flow API in Kotlin using
callbackFlow. And, this is the topic of this blog.
Here, I will take an example of a LocationManager just for the sake of understanding.
Assume that we can use LocationManager and listen to the changes in location as
below:
// create a location listener
val locationListener = object : LocationListener {

https://outcomeschool.com/blog/callback-to-flow-api-in-kotlin 2/5
6/24/25, 4:26 PM callbackFlow - Callback to Flow API in Kotlin

override fun onLocationUpdate(location: Location) {


// do something with the updated location
}

// register for location updates


LocationManager.registerForLocation(locationListener)

// unregister in onDestroy()
LocationManager.unregisterForLocation(locationListener)

Here, we have a LocationListener through which we get the onLocationUpdate callback.


Now, we want to use this LocationManager in the Flow API way.
Let's do this by creating a function which returns Flow<Location> as below:
fun getLocationFlow(): Flow<Location> {
return callbackFlow {

val locationListener = object : LocationListener {

override fun onLocationUpdate(location: Location) {


trySend(location)
}

LocationManager.registerForLocation(locationListener)

awaitClose {
LocationManager.unregisterForLocation(locationListener)
}

}
}

Now, it's time to understand what we have done to convert the Callback to Flow API.

https://outcomeschool.com/blog/callback-to-flow-api-in-kotlin 3/5
6/24/25, 4:26 PM callbackFlow - Callback to Flow API in Kotlin

We have followed the following steps to convert the Callback to Flow API in Kotlin:
Create a function to return the Flow<Location> .
Use callbackFlow as the return block.
Use trySend() for the location updates.
Use awaitClose block to unregister.
Now, we can use the above function as below:
launch {
getLocationFlow()
.collect { location ->
// do something with the updated location
}
}

This is how we can convert any callback to Flow API in Kotlin using callbackFlow.
So, today we learned how to convert any callback to Flow API and use them.
Prepare yourself for Android Interview: Android Interview Questions
That's it for now.
Thanks
Amit Shekhar
Co-Founder @ Outcome School
You can connect with me on:
Twitter
LinkedIn
YouTube
GitHub
Follow Outcome School on:
Twitter
https://outcomeschool.com/blog/callback-to-flow-api-in-kotlin 4/5

You might also like