简体   繁体   中英

Using webview callbacks in coroutines

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
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM