How to call a method after a delay in Android
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Android development, there are numerous scenarios where you might need to execute a method after a specified delay. For example, you might want to delay an action until an animation completes, or introduce a pause before initiating a network request after user input. This guide explores various ways to achieve timed method execution using Android's API.
Using Handler
One of the most common ways to introduce a delay in Android is through the Handler class. A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. It is used for scheduling messages or runnables to be executed at some point in the future.
Here is an example of how to use a Handler to call a method after a delay:
In this code:
- A new
Handleris created associated with the main thread's looper. postDelayedis used to schedule theRunnableto be executed after a specified delay, which is passed as the second parameter in milliseconds.
Using Timer and TimerTask
Another approach is using Timer and TimerTask. While Handler is often preferred because it is simpler and ties into the main thread’s message queue, Timer can be useful if you want more flexibility in managing repeated tasks.
Here's how to use Timer and TimerTask:
This setup involves:
- Creating an instance of
Timer. - Scheduling a
TimerTaskthat executes after a delay.
Using ScheduledExecutorService
For more complex scenarios, especially those needing precise control and configurations over thread management, ScheduledExecutorService is appropriate. It provides methods to schedule commands to run after a given delay or periodically.
Example:
This utilizes a single-threaded executor to delay execution. You specify the time unit alongside the delay time, improving code readability and preventing unit conversion errors.
Comparison of Methods
The following table summarizes the comparison between Handler, Timer, and ScheduledExecutorService:
| Feature | Handler | Timer | ScheduledExecutorService |
| Ease of Use | Simple | Relatively simple | Complex |
| Thread | Main thread or | Separate thread | Configurable per need |
| any specified Looper | |||
| Precision | Suitable for UI updates | Less precise | High precision |
| Scheduling flexibility | Fixed delay | Fixed delay or | Fixed delay or |
| fixed-rate | periodic execution | ||
| Resource management | Automatically tied | Manual management | Efficient management |
| to lifecycle | of resources |
Additional Considerations
- Main Thread: Always ensure that any UI updates are performed on the main thread.
Handlerlinked toLooper.getMainLooper()indeed runs on the main thread. - Memory Leaks: In the use of delayed operations, especially inside activities or fragments, be wary of memory leaks. Avoid anonymous inner classes holding a reference to the outer class where possible. Use static inner classes and weak references instead.
- Cancellation: Have mechanisms in place to cancel any ongoing operations if they are no longer needed, to prevent unwanted behavior and resource leaks.
Understanding when and how to use these different methods for executing delayed tasks in Android applications helps create efficient, highly performant, and user-friendly applications.
Related reading
- How to call a method with a separate thread in Java?
- How to call a method with a separate thread in Java?
- How to call a service from Main application calls Spring Boot?
- How to call getClass() from a static method in Java?
- How to call a method after a delay in Android
- How to call a SOAP web service on Android
- how to Call super constructor in Lombok
- How to capitalize the first letter of a String in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.