AsyncTask: The Deprecated Way to Do Background Work
AsyncTask was Android's original tool for short background tasks that update the UI. It was used for simple network requests or disk I/O. It's fully deprecated due to memory leaks and lifecycle issues; use Kotlin Coroutines instead.
Why it exists
Android's main UI thread cannot be blocked. Any long-running operation like a network call or database query will freeze the app, leading to an "Application Not Responding" (ANR) error. AsyncTask was created as a simple helper class to perform these operations on a background thread and publish the results back on the UI thread without manual thread management.
The mental model
Think of AsyncTask as a temporary worker you hire for a single job. You give them instructions on what to do in the background (doInBackground), they go away and work, and then they report the result back to you on the main thread (onPostExecute) so you can safely update the UI. You don't have to manage the communication details yourself.
How it works
AsyncTask uses generics to define types for its parameters, progress updates, and final result. It orchestrates work across four main steps: first, onPreExecute runs on the UI thread for setup; second, doInBackground runs on a background thread to perform the heavy lifting; third, onProgressUpdate can be called from the background to publish progress to the UI thread; and fourth, onPostExecute delivers the final result to the UI thread.
When to use it
You should not use AsyncTask in new code. Its only modern relevance is in maintaining very old Android applications that were written before its deprecation and have not yet been migrated to modern concurrency patterns like Kotlin Coroutines.
When not to use it
Do not use AsyncTask in any new development, as it was deprecated in Android 11 (API 30). It is infamous for causing memory leaks because it holds a reference to the hosting Activity or Fragment. If the screen is rotated, the old Activity is destroyed but AsyncTask keeps it in memory. It's a poor choice for long-running tasks. Use Kotlin Coroutines with ViewModel and LiveData/Flow instead.
One canonical example
A classic use case was downloading an image from a URL. The doInBackground method would take the URL string, open an HTTP connection, and decode the image into a Bitmap. The onPostExecute method would then receive this Bitmap on the main thread and set it on an ImageView. This entire pattern is now better and more efficiently handled by image-loading libraries like Glide or Coil.
Interview question
Which of the following is the primary reason AsyncTask was deprecated for new Android development?
- a.It frequently resulted in memory leaks and difficulties managing UI updates across configuration changes.Correct
- b.It was unable to perform complex network operations or database queries efficiently.
- c.It required manual thread management, making it overly complicated for simple background tasks.
- d.It often caused the application to freeze and display "Application Not Responding" (ANR) errors.
Why? this is the answer
The card states AsyncTask was deprecated because it was "infamous for causing memory leaks" and had "lifecycle issues" due to holding references to UI components. Its purpose was to prevent ANRs by moving work off the main thread, not cause them, and it simplified thread management.
Just read this? Test yourself on what you have been reading.
Read the original → developer.android.com
- #android
- #kotlin
- #concurrency
- #deprecated
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.
See open roles