1

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

testivanivan
  • 967
  • 13
  • 36
  • This seems to have been asked before already: https://stackoverflow.com/questions/66317069/handler-or-launching-a-coroutine-job-to-do-something-in-the-mainthread. – user3738870 Nov 02 '22 at 10:13
  • What do you need the "not immediately" for? – TheLibrarian Nov 02 '22 at 11:01
  • @TheLibrarian, please, see this answer https://stackoverflow.com/a/41953519/15742980 – testivanivan Nov 02 '22 at 11:40
  • I think the question is about a solution to your problem and not as much about the problem itself. For example is the condition you are checking in the second fragment available only there and then and not before that? – TheLibrarian Nov 02 '22 at 12:35

1 Answers1

1

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.

BPDev
  • 397
  • 1
  • 9