Handler.postDelayed (Runnable) vs CountdownTimer

Sometimes we need to delay the code until it runs.

This can be done using Handler.postDelayed(Runnable) or CountdownTimer .

Which one is better in terms of performance?

See code example below

Handler

  new Handler().postDelayed(new Runnable() { @Override public void run() { //DO SOMETHING } }, 1000); 

CountDownTimer

  new CountDownTimer(1000, 1000) { public void onFinish() { //DO SOMETHING } public void onTick(long millisUntilFinished) {} }.start(); 
+5
source share
3 answers

Handler should offer you the best results, since CountDownTimer contains itself Handler , as you can see here .

+5
source

Use Handler, Android Handler is good.

See here what others say about the handler

+1
source

I agree that Handler offers the best performance. But on the side of the note, you should keep in mind that the CountDownTimer object will be destroyed upon completion. After completion, the handler will continue to exist. If you only need a temporary timer, then CountDownTimer is preferable. Otherwise, use a handler.

0
source

Source: https://habr.com/ru/post/1243367/


All Articles