Need an explanation how to use AsyncTask?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
AsyncTask was Android's old helper for running short background work and posting results back to the main thread. It is deprecated now, so you should learn it mainly to maintain legacy code rather than to start new work. The important idea is its lifecycle: setup on the UI thread, work in the background, then deliver progress or results back to the UI thread safely.
What the Type Parameters Mean
AsyncTask is usually declared with three generic types:
They mean:
- '
Params: values passed toexecute(...)' - '
Progress: values sent throughpublishProgress(...)' - '
Result: value returned fromdoInBackground(...)'
That structure explains the whole API.
The Lifecycle Methods
A typical task uses these methods:
- '
onPreExecute()runs on the UI thread before work starts.' - '
doInBackground(...)runs on a background thread.' - '
onProgressUpdate(...)runs on the UI thread afterpublishProgress(...).' - '
onPostExecute(...)runs on the UI thread when the background work finishes.' - '
onCancelled()runs on the UI thread if the task is cancelled.'
The rule is simple: long-running work belongs in doInBackground(...), and UI updates belong in the UI-thread callbacks.
A Legacy Example
The WeakReference matters because a non-static inner task can leak the activity.
When AsyncTask Was a Good Fit
Historically, AsyncTask was used for short operations such as:
- small network calls in old apps
- parsing local files
- simple database work
- updating progress bars for brief tasks
It was never a great fit for long-running, retryable, or lifecycle-sensitive background jobs.
Why It Causes Problems
Most AsyncTask bugs come from lifecycle mismatch. The task keeps running while the activity rotates, finishes, or gets destroyed. Then a UI callback tries to update views that no longer exist.
That is why legacy code should keep tasks:
- static or otherwise lifecycle-safe
- short-lived
- cancel-aware
- careful about holding activity references
What to Use Instead in New Code
Modern Android code normally uses:
- Kotlin coroutines with
lifecycleScopefor UI-related asynchronous work - '
WorkManagerfor deferrable or retryable background jobs' - executors or structured concurrency primitives for lower-level control
So if the real question is "what should I use today," the answer is usually not AsyncTask.
Common Pitfalls
- Updating views directly from
doInBackground(...)instead of using UI-thread callbacks. - Keeping a strong reference to an activity and leaking it across configuration changes.
- Using
AsyncTaskfor long-running or durable jobs that should really useWorkManager. - Ignoring cancellation and letting the task continue after the screen is gone.
- Learning
AsyncTaskas a modern Android pattern instead of as legacy maintenance knowledge.
Summary
- '
AsyncTasksplits work into UI-thread setup, background execution, and UI-thread result handling.' - '
doInBackground(...)is for background work andonPostExecute(...)is for final UI updates.' - Legacy implementations should avoid leaking activities and should handle cancellation.
- '
AsyncTaskis deprecated and should not be the default choice for new Android code.' - Learn it mainly to understand or migrate old codebases.
Related reading
- Need help implementing async calls in C 4.0 Web Service Client
- Need sample fire and forget async call to WCF service
- need some clarifications about dispatch queue, thread and NSRunLoop
- Need to understand the usage of SemaphoreSlim
- Need to use Apache Kafka in my React-Native app
- New Array from Index Range Swift
- Nested Async Await Does not Wait
- Nested Async/Await Doesn't Appear To Be Scaling
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free 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.