5.3.11. Using a Handler to Execute a Runnable in the Future

When the user makes a correct guess, the app displays the correct answer for two seconds before displaying the next flag. To do this, we use a Handler (package android.os). Handler method postDelayed receives as arguments a Runnable to execute and a delay in milliseconds. After the delay has passed, the Handler’s Runnable executes in the same thread that created the Handler. Operations that interact with or modify the GUI must be performed in the GUI thread, because GUI components are not thread safe.

Get Android™ How to Program, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.