'constructor Handler()' is deprecated. Deprecated in Java, How to use Handler() class in Android?

'constructor Handler()' is deprecated, when i target Android version 30, it return this deprecated wraning for Handler()

To resolve this first we need to know What is Handler

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue

 

Handle will be use in two ways in Android application

  • Schedule and execute a messages at future at some point
  • Enqueue action ue an action to be performed on a different thread

Handler() introduced in Android 1 and usage of default constructor was deprected in Andorid 30.

To work with handler in Android 30 we need to pass Looper object inside contructor

 

Handler(Looper.getMainLooper()).postDelayed({
    // Your Code
}, 3000)