Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4133 pages

Upgrades and deprecations

AsyncTask Is Deprecated: The Android Alternative for Each Job

In short

The Android AsyncTask deprecated alternative depends on one question: must the work finish after the user leaves the screen? If not, use Kotlin coroutines in viewModelScope, or an ExecutorService with a main-thread Handler in Java. If it must finish, schedule it with WorkManager. A straight port keeps AsyncTask's leaks and lost results.

Why did Google deprecate AsyncTask, and does old code still run?

The reference page marks the class deprecated in API level 30, after it had existed since API level 3. Its replacement advice is a single line: use the standard java.util.concurrent or Kotlin concurrency utilities instead.

The reasons it gives are the useful part. Tied to UI, AsyncTask caused Context leaks, missed callbacks and crashes on configuration changes. It behaved differently across platform versions, it swallowed exceptions thrown in doInBackground, and it added little over using Executors directly.

The class is still documented, and the page does not say it was removed. The two executors it exposes, SERIAL_EXECUTOR and THREAD_POOL_EXECUTOR, carry the same API level 30 deprecation. So existing code keeps compiling with warnings; the bugs the page lists are the reason to move, not a removal date.

Checked against: source 1

What is the Android AsyncTask deprecated alternative for each job?

Android's background-work overview splits tasks into three kinds, and the split is the decision. I would sort every AsyncTask in a codebase into one of these before rewriting any of them.

  • Work the user is waiting on while the screen is open, such as loading a list: asynchronous work. The overview names Kotlin coroutines and Java threads, and warns that this kind of work is not guaranteed to finish once the app leaves a valid lifecycle stage.
  • Work that has to finish even if the user leaves, such as an upload or a periodic sync: the task scheduling APIs. The overview calls WorkManager the best option in most cases, with JobScheduler for some, and lets urgent jobs be marked expedited.
  • Work that must start now and must not be interrupted: a foreground service, which the overview says can put a heavy load on the device and carries privacy and security implications.
  • Anything AsyncTask's own page would rule out: it says tasks should last a few seconds at most, and that longer-running threads belong on Executor, ThreadPoolExecutor or FutureTask.

Checked against: source 1, source 2

How do AsyncTask's four callbacks translate?

AsyncTask bundled four steps, and each one has a different home in the replacements. The mapping below follows the official Kotlin and Java guides.

In Kotlin, with coroutines

Android's coroutines page calls coroutines the recommended solution for asynchronous programming on Android. onPreExecute becomes ordinary code before the call. doInBackground becomes a suspend function that wraps its blocking part in withContext(Dispatchers.IO), which the guide calls making the function main-safe.

onPostExecute is simply the line after that call, because the coroutine resumes where it was launched. Launch it in viewModelScope, which the lifecycle guide says is cancelled when the ViewModel is cleared, taking running coroutines with it. That replaces the manual cancel() and isCancelled() checks.

Errors become visible again. Where AsyncTask swallowed exceptions, the guide wraps the call in a plain try/catch and turns the failure into an error state for the UI.

In Java, with an Executor and a Handler

The Java threads guide builds a thread pool with Executors.newFixedThreadPool(4) in its example, and says to create it once when the app starts because threads are expensive. doInBackground becomes a Runnable passed to executor.execute().

There is no automatic onPostExecute. The guide points out that a callback invoked from the pool runs on that background thread, so you post the result back through a Handler built on Looper.getMainLooper(), for example with HandlerCompat.createAsync. From a ViewModel, it suggests MutableLiveData.postValue() instead.

onProgressUpdate has no named counterpart in either guide. I would publish progress the same way as the result: a posted Handler message in Java, or a value the UI observes in Kotlin.

Checked against: source 1, source 3, source 4, source 5

What do teams get wrong when they port AsyncTask?

The most common mistake is a mechanical port. Swapping AsyncTask for a raw Thread or an Executor inside an Activity keeps the exact Context leak and lost result the reference page lists. The fix is where the work lives, in a ViewModel or a repository, more than which API runs it.

The second is parallelism. The reference page says AsyncTask has run tasks on a single thread since Honeycomb unless you called executeOnExecutor with THREAD_POOL_EXECUTOR. A fixed pool of four, as in the Java guide, runs things in parallel, so code that quietly relied on one-at-a-time ordering can start racing.

The third is putting must-finish work into a coroutine or an Executor. The overview says asynchronous work can stop when the app leaves the foreground. If the result matters after that, it is a WorkManager job, whatever AsyncTask used to do.

Checked against: source 1, source 2, source 3

What else do people ask?

Is RxJava an official replacement?

None of the Android pages cited here name it. The AsyncTask reference names java.util.concurrent and Kotlin concurrency, and the coroutines page calls coroutines the recommended solution.

Can I just suppress the deprecation warning?

The code still compiles and the page does not announce a removal. The reasons for the deprecation, leaks and lost callbacks on configuration change, stay in the app whether the warning shows or not.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. This page joins Android documentation I fetched on 20 Nov 2026 (the AsyncTask reference, the background-work overview, and the Java threads, coroutines and lifecycle guides). I did not port an app for it. It does not cover RxJava, WorkManager setup, foreground service types or testing. Found a mistake? Tell me.

Keep reading

Next step

Have a project in mind?

Tell me about it. A discovery call within 48 hours.