Android Development
Programming Solutions
Deprecation
Code Optimization
API Updates

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:

java
Handler handler = new Handler();

with an explicit looper:

java
Handler handler = new Handler(Looper.getMainLooper());

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:

java
1import android.os.Handler;
2import android.os.Looper;
3
4Handler mainHandler = new Handler(Looper.getMainLooper());
5mainHandler.post(() -> {
6    System.out.println("Running on the main thread");
7});

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:

  • Executor or ExecutorService for Java concurrency
  • Kotlin coroutines for modern Kotlin apps
  • WorkManager for guaranteed deferrable background work

For a one-off task, an executor is often clearer than a background HandlerThread:

java
1import java.util.concurrent.ExecutorService;
2import java.util.concurrent.Executors;
3
4ExecutorService executor = Executors.newSingleThreadExecutor();
5executor.execute(() -> {
6    System.out.println("Background work");
7});

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 Message objects
  • working on a dedicated looper thread

For a handler on a non-main looper, create or obtain that looper explicitly:

java
1import android.os.Handler;
2import android.os.HandlerThread;
3
4HandlerThread thread = new HandlerThread("worker");
5thread.start();
6
7Handler workerHandler = new Handler(thread.getLooper());
8workerHandler.post(() -> {
9    System.out.println("Running on HandlerThread");
10});

View and Activity helpers

When the work is tied to a UI element, helper APIs avoid manual looper plumbing:

java
button.post(() -> button.setText("Updated"));

Or in an Activity:

java
runOnUiThread(() -> {
    // update UI
});

Async handlers

If the handler's messages must not be blocked by synchronization barriers such as display vsync, Android offers async handler creation:

java
Handler asyncHandler = Handler.createAsync(Looper.getMainLooper());

This is a specialized tool, not a general replacement, but it is useful when UI scheduling behavior matters.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design