-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Creating and Executing Async Tasks
AsyncTask is a mechanism for executing operations in a background thread without having to manually handle thread creation or execution. AsyncTasks were designed to be used for short operations (a few seconds at the most) and you might want to use a Service and/or Executor for very long running tasks.
This is typically used for long running tasks that cannot be done on UIThread, such as downloading network data from an API or indexing data from elsewhere on the device. An AsyncTask
streamlines the following common background process:
- Pre - Execute code on the UI thread before starting a task (e.g show ProgressBar)
- Task - Run a background task on a thread given certain inputs (e.g fetch data)
- Updates - Display progress updates during the task (optional)
- Post - Execute code on UI thread following completion of the task (e.g show data)
See Displaying Remote Images the Hard Way for an example of how AsyncTask can be used for retrieving a remote image.
Creating an AsyncTask is as simple as defining a class that extends from AsyncTask
such as:
// The types specified here are the input data type, the progress type, and the result type
private class MyAsyncTask extends AsyncTask<String, Progress, Bitmap> {
protected void onPreExecute() {
// Runs on the UI thread before doInBackground
// Good for toggling visibility of a progress indicator
progressBar.setVisibility(ProgressBar.VISIBLE);
}
protected Bitmap doInBackground(String... strings) {
// Some long-running task like downloading an image.
Bitmap = downloadImageFromUrl(strings[0]);
return someBitmap;
}
protected void onProgressUpdate(Progress... values) {
// Executes whenever publishProgress is called from doInBackground
// Used to update the progress indicator
progressBar.setProgress(values[0]);
}
protected void onPostExecute(Bitmap result) {
// This method is executed in the UIThread
// with access to the result of the long running task
imageView.setImageBitmap(result);
// Hide the progress bar
progressBar.setVisibility(ProgressBar.INVISIBLE);
}
}
The worker once defined can be started anytime by creating an instance of the class and then invoke .execute
to start the task:
public void onCreate(Bundle b) {
// ...
// Initiate the background task
downloadImageAsync();
}
private void downloadImageAsync() {
// Now we can execute the long-running task at any time.
new MyAsyncTask().execute("http://images.com/image.jpg");
}
AsyncTask accepts three generic types to inform the background work being done:
-
AsyncTask<Params, Progress, Result>
-
Params
- the type that is passed into the execute() method. -
Progress
- the type that is used within the task to track progress. -
Result
- the type that is returned by doInBackground().
-
For example AsyncTask<String, Void, Bitmap>
means that the task requires a string input to execute, does not record progress and returns a Bitmap after the task is complete.
AsyncTask has multiple events that can be overridden to control different behavior:
-
onPreExecute
- executed in the main thread to do things like create the initial progress bar view. -
doInBackground
- executed in the background thread to do things like network downloads. -
onProgressUpdate
- executed in the main thread whenpublishProgress
is called from doInBackground. -
onPostExecute
- executed in the main thread to do things like set image views.
An AsyncTask
is tightly bound to a particular Activity
. In other words, if the Activity
is destroyed or the configuration changes then the AsyncTask
will not be able to update the UI on completion. As a result, for short one-off background tasks tightly coupled to updating an Activity, we should consider using an AsyncTask
as outlined above. A good example is for a several second network request that will populate data into a ListView
within an activity.
Using the AsyncTask
is the easiest and most convenient way to manage background tasks from within an Activity. However, in cases where tasks need to be processed in parallel with more control, or the tasks need to continue executing even when the activity leaves the screen, you'll need to create a background service or manage threaded operations more manually.
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.