Handler.postDelayed(Runnable) vs CountdownTimer

JayVDiyk picture JayVDiyk · Feb 19, 2016 · Viewed 7.1k times · Source

Sometimes we need to delay a code before it runs.

This is doable by the Handler.postDelayed(Runnable) or CountdownTimer.

Which one is better in terms of performance?

See the sample code 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();

Answer

E-Kami picture E-Kami · Feb 19, 2016

The Handler should offer you better performances as CountDownTimer contains itself a Handler as you can see here.