Android, Kotlin: Handler.post vs coroutines

2k views Asked by At

I use Handler(Looper.getMainLooper()).post(runnable) which places the query on the end of the thread queue instead of running it immediately. Is it ok to use this with kotlin, or is it better to use coroutines now (like lifecycleScope.launchWhenResumed {}). Or in these cases, you can leave it like that?

Please, help me

1

There are 1 answers

0
BPDev On

Coroutines need to be mapped by a dispatcher to a thread. I think that Dispatchers.Main uses handlers to do it (details), so there shouldn't be a major change in performance.

Coroutines are lifecycle aware and you will have to use them anyway if you use libraries with suspend functions (I doubt there is a workaround?), but you can still use handlers.