Skip to content

Handler postDelayed uses in Kotlin

  • by
Handler postDelayed uses in Kotlin

1. Overview

In this article, we will learn about the uses of the postDelayed method of the Handler class along with examples in Kotlin.

Normally, Android apps use the main thread to handle UI tasks and input events. This thread collects all these input events or messages in a queue (MessageQueue) and then processes them using an instance of the Looper class. By default, the main thread already has a Looper prepared.

This Looper keeps the main thread alive and manages the message queue. It then takes each message from the queue and executes it on the thread.

A Handler gives you a mechanism to push tasks into this queue from any other threads thus allowing other threads to communicate with the UI thread. However, it doesn’t mean that the Handler supports only the main thread. It can push tasks to any thread’s queue that has a Looper.

You can use Handler to add a Runnable object or messages to the Looper to execute the code on the Looper‘s thread.

See Handler in Kotlin article to know more about the Handler. Here, we focus on postDelayed method.

2. Handler postDelayed uses in Kotlin

The syntax of the postDelayed method of the Handler class is:

public final boolean postDelayed (Runnable r, long delayMillis)

The postDelayed method takes two parameters Runnable and delayMillis. It adds the Runnable to the thread’s message queue to be run after the specified amount of time elapses.

The Runnable will execute on the thread to which this handler is attached. If the thread goes to deep sleep, then it adds an additional delay to the execution of the Runnable.

3. Handler postDelayed examples in Kotlin

3.1. Run handler in main thread after a delay

Looper includes a helper function, getMainLooper(), which retrieves the Looper of the main thread. You can run the Runnable code in the main thread after 5 ms by using the postDelayed function.

 val mainHandler = Handler(Looper.getMainLooper()).postDelayed ({
            System.out.println("Thread : " + Thread.currentThread().name)
        }, 5000)

3.2. Run handler in the local thread after a delay

To execute the handler in the current thread, retrieve the Looper of the current thread by using the Looper.myLooper method. Therefore, you can push the Runnable in the local thread’s message queue for later processing.

 val mainHandler = Handler(Looper.myLooper()).postDelayed ({
            System.out.println("Thread : " + Thread.currentThread().name)
        }, 5000)

3.3. Specify the Looper of any thread

Alternatively, you can create a thread with Looper and execute the task in that created thread.

val handlerThread = HandlerThread("HandlerThread")
handlerThread.start()

val backgroundHandler = Handler(handlerThread.looper).postDelayed ({
    println("Thread : " + Thread.currentThread().name)
    handlerThread.quitSafely(); 
}, 1000)

3. Conclusion

To sum up, we have learned the Handler postDelayed method uses along with examples in Kotlin.

Leave a Reply

Your email address will not be published. Required fields are marked *