Using webview callbacks in coroutines

Viewed 37

I have a situation where I have a suspend function in which I want to call a webview and receive data in its shouldOverrideUrlLoading and continue the coroutine after receiving the response in the webview URL.

I have not found anything useful yet. So I am confused as to how to implement this. Also, how will I switch the thread to Dispatchers.Main from Dispatchers.IO

Sudo code of what is required

withContext(Dispatchers.Main) {
    call api1
    call api2

    launch webview in main thread
    get result from shouldOverrideUrlLoading

    call api3 with the result from above 
    return result of api3
}
1 Answers

You can use suspendCancellableCoroutine. It is designed to turn callback APIs into suspend functions in a simple way. You can find some examples of how to use it here : https://kt.academy/article/cc-cancellation.

Related