3

I use this function to delay the notifyDatasetChanged() function.

Handler().postDelayed({
    notifyDataSetChanged()
}, 100)

Handler().postDelayed is now deprecated. What function to call instead ?

In the documentation, Google says :

This constructor is deprecated. Implicitly choosing a Looper during Handler construction can lead to bugs where operations are silently lost (if the Handler is not expecting new tasks and quits), crashes (if a handler is sometimes created on a thread without a Looper active), or race conditions, where the thread a handler is associated with is not what the author anticipated. Instead, use an Executor or specify the Looper explicitly, using Looper#getMainLooper, {link android.view.View#getHandler}, or similar. If the implicit thread local behavior is required for compatibility, use new Handler(Looper.myLooper(), callback) to make it clear to readers.

But I am a beginner and I don't speak English language well enough, and I can't understand if there is an other option to replace handler().

Zain
  • 37,492
  • 7
  • 60
  • 84
jujuf1
  • 175
  • 2
  • 10

2 Answers2

10

You need to use the constructor with an explicit looper, for the Main thread Looper use Looper.getMainLooper()

Handler(Looper.getMainLooper()).postDelayed({
    notifyDataSetChanged()
}, 100)
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Zain
  • 37,492
  • 7
  • 60
  • 84
-4

I highly recommend to use Kotlin Coroutines for that. After you Implemented the library into your project, this is the example code of Coroutines:

      GlobalScope.launch {
          delay(2000)
          "Your Code"
      }
A.R.B.N
  • 1,035
  • 2
  • 10
  • 20