Android Handler() deprecated: what to use instead
Codemia Knowledge Hub ·
Handler itself is not deprecated on Android. Only the implicit constructors, new Handler() and new Handler(callback), are, because they silently pick the current thread's Looper and can attach your handler to the wrong thread or to no valid looper at all. Pass the looper explicitly, usually new Handler(Looper.getMainLooper()), or use a higher-level concurrency tool that fits the job.
The direct replacement
Replace this:
with an explicit looper:
That one change removes most of the ambiguity. Watch for new Handler() in utility code that may run on threads without a looper; that fragile behavior is exactly what the deprecation is meant to surface.
Posting work to the main thread
To run code on the UI thread, use a handler on the main looper:
This is still valid Android code, and the target thread is now obvious.
Often you do not need Handler at all
Many older samples used Handler as a generic async tool. For background work, prefer APIs that express it directly:
ExecutororExecutorServicefor Java concurrency- Kotlin coroutines for modern Kotlin apps
WorkManagerfor guaranteed deferrable background work
For a one-off task, an executor is often clearer than a background HandlerThread:
Do not swap every Handler for an executor blindly, though. Executors run tasks, but they are not message-loop primitives, so check whether the code needs a looper-backed message queue. And for long-running or guaranteed background work, WorkManager or other lifecycle-aware tools are usually more appropriate than a Handler.
When Handler is still the right tool
Handler fits when you work with an existing Looper and message queue:
- posting back to the main thread
- interoperating with APIs that already use
Messageobjects - working on a dedicated looper thread
For a handler on a non-main looper, create or obtain that looper explicitly:
View and Activity helpers
When the work is tied to a UI element, helper APIs avoid manual looper plumbing:
Or in an Activity:
Async handlers
If the handler's messages must not be blocked by synchronization barriers such as display vsync, Android offers async handler creation:
This is a specialized tool, not a general replacement, but it is useful when UI scheduling behavior matters.
Related reading
- What does --logtostderr mean in the command line while using tensorflow's object detection api?
- What does --networkhost option in Docker command really do?
- What does 0.0.0.0/0 and /0 mean?
- What does %5B and %5D in POST requests stand for?
- What do we mean by top percentile or TP based latency?
- What do you do without fast gather and scatter in AVX2 instructions?
- What does 0 and 1 mean in Swift Closures?
- What does addChildViewController actually do?
System design interview problems on this topic
- Design a Load Balancer: solution and walkthrough
- Design a Domain Name System: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.