callbackFlow - Callback to Flow API in Kotlin
Tags: kotlin, android
Published: 2023-01-31 05:31:27

---
title: callbackFlow - Callback to Flow API in Kotlin
published: true
description: In this blog, we will learn how to convert any Callback to Flow API in Kotlin using callbackFlow.
tags: kotlin, android
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/douqrsuidi1w1nm8hzvl.png
canonical_url: https://amitshekhar.me/blog/callback-to-flow-api-in-kotlin
---

I am [**Amit Shekhar**](https://amitshekhar.me), a mentor helping developers in getting high-paying tech jobs.

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out [Android Interview Questions and Answers](https://www.youtube.com/playlist?list=PL_I3TGB7aK6jNBMZkw3FYdJXyf7quHdI8).

In this blog, we will learn how to convert any Callback to Flow API in Kotlin using callbackFlow.

**This article was originally published at [amitshekhar.me](https://amitshekhar.me/blog/callback-to-flow-api-in-kotlin).**

This blog is a part of the series I have written on **Flow API in Kotlin**:

- [Mastering Flow API in Kotlin](https://amitshekhar.me/blog/flow-api-in-kotlin)
- [Creating Flow Using Flow Builder in Kotlin](https://amitshekhar.me/blog/creating-flow-using-flow-builder-in-kotlin)
- [Terminal Operators in Kotlin Flow](https://amitshekhar.me/blog/terminal-operators-in-kotlin-flow)
- [Cold Flow vs Hot Flow](https://amitshekhar.me/blog/cold-flow-vs-hot-flow)
- [StateFlow and SharedFlow](https://amitshekhar.me/blog/stateflow-and-sharedflow)
- [Long-running tasks in parallel with Kotlin Flow](https://amitshekhar.me/blog/long-running-tasks-in-parallel-with-kotlin-flow)
- [Retry Operator in Kotlin Flow](https://amitshekhar.me/blog/retry-operator-in-kotlin-flow)
- [Retrofit with Kotlin Flow](https://amitshekhar.me/blog/retrofit-with-kotlin-flow)
- [Room Database with Kotlin Flow](https://amitshekhar.me/blog/room-database-with-kotlin-flow)
- [Kotlin Flow Zip Operator for Parallel Multiple Network Calls](https://amitshekhar.me/blog/kotlin-flow-zip-operator-parallel-multiple-network-calls)
- [Instant Search Using Kotlin Flow Operators](https://amitshekhar.me/blog/instant-search-using-kotlin-flow-operators)
- [Exception Handling in Kotlin Flow](https://amitshekhar.me/blog/exception-handling-in-kotlin-flow)
- callbackFlow - Callback to Flow API in Kotlin - **YOU ARE HERE**
- [Unit Testing ViewModel with Kotlin Flow and StateFlow](https://amitshekhar.me/blog/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:

```kotlin
// create a location listener
val locationListener = object : LocationListener {

    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:

```kotlin
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.

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:

```kotlin
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.

Master Kotlin Coroutines from here: [Mastering Kotlin Coroutines](https://amitshekhar.me/blog/kotlin-coroutines)

That's it for now.

Thanks

[**Amit Shekhar**](https://amitshekhar.me)

You can connect with me on:

- [Twitter](https://twitter.com/amitiitbhu)
- [YouTube](https://www.youtube.com/@amitshekhar)
- [LinkedIn](https://www.linkedin.com/in/amit-shekhar-iitbhu)
- [GitHub](https://github.com/amitshekhariitbhu)

[**Read all of my high-quality blogs here.**](https://amitshekhar.me/blog)