AsyncTask Android example
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
AsyncTask in Android is a framework that allows developers to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. This class is designed to be a straightforward and easy-to-use utility for handling tasks that require background processing but still need to interact with the user interface.
Basic Structure of AsyncTask
AsyncTask is an abstract class, and it needs to be subclassed to perform the actual task. It requires us to define a few methods and has specific type parameters that need to be specified.
Here is the basic structure of AsyncTask:
Parameters
- Params: The type of the input parameters sent to the task upon execution.
- Progress: The type of the progress units published during the background computation.
- Result: The type of the result of the background computation.
Key Methods
onPreExecute(): Invoked on the UI thread before the task is executed. This method can be used to set up the task, like showing a progress bar in the UI.doInBackground(Params... params): This is the core method that is invoked on the background thread and is where the time-consuming background operations are performed. This needs to be overridden to perform the task.onProgressUpdate(Progress... values): This method is invoked on the UI thread after a call topublishProgress(Progress...). This can be used to update the progress in the UI, like updating a progress bar.onPostExecute(Result result): This method is invoked on the UI thread after the background computation finishes. It is used to report the results of the computation.
Example
Let's look at a simple example of AsyncTask that downloads data from the web.
Considerations
- Threading Constraints:
AsyncTaskmust be invoked on the UI thread. This is important as the lifecycle methods ofAsyncTaskinteract with UI components and hence must be executed on the main thread. - Cancelable Tasks:
AsyncTaskcan be canceled with thecancel(Boolean)method. After canceling,isCancelled()can be checked during the task progress to halt operations. - Lifecycle Awareness:
AsyncTaskis not lifecycle-aware, which means it does not handle configuration changes like screen rotations. This could lead to memory leaks if the activity or fragment is recreated during a long-running task. - Limited Parallelism: By default,
AsyncTaskuses a single worker thread, which can be a limitation in scenarios with high concurrency requirements.
Alternatives to AsyncTask
Given the limitations and after its deprecation, developers are encouraged to use other alternatives such as:
- Java
ExecutorService: For more flexible task execution, allowing various scheduling and management features. HandlerThread: A simple way to perform background work on a dedicated thread with a message loop.RxJava: A reactive programming library that offers a powerful means to perform asynchronous operations.- Android
WorkManager: A high-level library for background work, recommended for tasks that require guaranteed execution.
Summary of AsyncTask Features
| Feature | Description |
| Background Execution | Executes code in a background thread. |
| UI Thread Interaction | Allows updates on UI thread post-execution. |
| Progress Updates | Supports progress updates via publish/update logic. |
| Deprecation | Deprecated in later Android releases. |
| Lack of Lifecycle Awareness | Not designed for handling configuration changes. |
| Limited Parallel Execution | Does not inherently support high concurrency. |
In conclusion, while AsyncTask provides a straightforward means to perform background operations in Android, its use is limited by several constraints, and developers are nudged towards more modern and powerful alternatives as it has become deprecated in favor of better solutions.

