# Lifecycle-aware delayed actions in Android Views with Kotlin Coroutines

DevFeed: [Lifecycle-aware delayed actions in Android Views with Kotlin Coroutines](<https://devfeed.tech/articles/safe-delay-in-android-views-goodbye-handlers-hello-coroutines-25940.md>)

Original publisher: [Read original article](<https://juliensalvi.medium.com/safe-delay-in-android-views-goodbye-handlers-hello-coroutines-cd47f53f0fbf?source=rss-cbfc736ddcd3------2>)

Author: Julien Salvi

Published: 2021-08-08T13:22:20Z

Content type: tutorial

Language: en

Sources: [Stories by Julien Salvi on Medium](<https://devfeed.tech/sources/stories-by-julien-salvi-on-medium.md>)

Topics: [Android](<https://devfeed.tech/topics/android.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [kotlin-coroutines](<https://devfeed.tech/topics/kotlin-coroutines.md>), [Library](<https://devfeed.tech/topics/library.md>), [ui](<https://devfeed.tech/topics/ui.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [deprecated](<https://devfeed.tech/tags/deprecated.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-coroutines](<https://devfeed.tech/tags/kotlin-coroutines.md>), [lifecycle](<https://devfeed.tech/tags/lifecycle.md>), [ui](<https://devfeed.tech/tags/ui.md>)

## AI overview

This tutorial explains why delayed actions posted through Android Handlers or Views can run after a view is detached or destroyed. It demonstrates replacing Handler().postDelay() with Kotlin coroutines tied to a view's lifecycle, using the lifecycle owner, a lifecycle coroutine scope, the Main Dispatcher, and delay().

## Source excerpt

Using Handlers, without providing a Looper, has been deprecated in recent versions of Android because they can lead to bugs where operations are silently lost or crashes. According to the documentation, you can use an Executor or access the Handler of a view as a replacement, but they can also crash silently as it's not bound to any lifecycle. Let's see how we can take advantage of the coroutines and lifecycle libraries to replace the Handler().postDelay() method to safely trigger an action after a specified amount of time. Handler: old fashioned way Even if the Handler() constructor is deprecated, you can access the handler of a View by calling getHandler() or you can directly call the method postDelay() on the View. That way, you won't use any deprecated method and you will be able to delay you action with a given amount of time. Let's see how it works under the hood. To push new events, you will get access to an Handler that is associated with the thread running the view. Every time a new action will be posted with postDelay(), a new Runnable will be added to the message queue (remember that the runnable will be running on the UI thread). By doing this, your delayed action may run when the view is detached or destroyed and this can lead to unexpected behaviors or crashes. Kotlin + Coroutine + Lifecycle 💚 Let's see how we can implement a better solution than Handler().postDelay() using Kotlin, Coroutines and the lifecycle library. First, make sure that you have these dependencies in your project: Then, we are ready to implement the function. To achieve a safe delay, we'll need to find the lifecycleOwner of the view thanks to the findViewTreeLifecycleOwner() method provided by the lifecycle library. Then, we can access the coroutine scope of the lifecycle to launch a new coroutine with a given Dispatcher (we'll be using the Main Dispatcher by default as we want to perform actions in the UI thread). To trigger the work in the coroutine in a specified amount of time,