How to solve Handler() deprecated?

后端 未结 12 1746
眼角桃花
眼角桃花 2020-12-05 09:00

Could anyone know, how to fix the deprecated warning or any alternate solution for this.

Handler().postDelayed({
    context?.let {
        //code
    }
}, 30         


        
12条回答
  •  感动是毒
    2020-12-05 09:36

    According to the document (https://developer.android.com/reference/android/os/Handler), "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()) to make it clear to readers."

    we should quit using the constructor without a Looper, specific a Looper instead.

提交回复
热议问题