Avatar ·

What to use instead of deprecated Handler() in Kotlin/Android?

Hello everyone! I'm a beginner in Android development with Kotlin and I've run into a problem. Previously, I used `Handler()` to execute tasks with a delay or switch to the main thread from a background thread. For example, I had code like this:\n\n```kotlin\nval handler = Handler()\nhandler.postDelayed({\n // some code after 2 seconds\n}, 2000)\n```\n\nBut recently I updated the SDK, and now the IDE underlines `Handler()` in red — it says the constructor is deprecated. The documentation says to use `Looper.getMainLooper()`, but I don't fully understand how to do it correctly.\n\nI've also heard about `HandlerThread` and `Coroutines`, but I don't know which is better for simple tasks like delaying before updating the UI. Can you explain how to properly write code that was previously done with `Handler()`?