Skip to content

Creating and Executing Async Tasks

Nathan Esquenazi edited this page Aug 16, 2013 · 15 revisions

Overview

AsyncTask is a mechanism for executing long running operations in a background thread without having to manually handle thread creation or execution.

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.

Defining an AsyncTask

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, Void, Bitmap> {
     protected void onPreExecute() {
         // Runs on the UI thread before doInBackground
         // Good for toggling visibility of a progress indicator
     }

     protected Bitmap doInBackground(String... strings) {
         // Some long-running task like downloading an image.
         return someBitmap;
     }

     protected void onProgressUpdate(Progress... values) {
        // Executes whenever publishProgress is called from doInBackground
        // Used to update the progress indicator
     }  

     protected void onPostExecute(Bitmap result) {
         // This method is executed in the UIThread
         // with access to the result of the long running task
     }
}

private void downloadImageAsync() {
    // Now we can execute the long-running task at any time.
    new MyAsyncTask().execute("http://images.com/image.jpg");
}

AsyncTask accept 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 when publishProgress is called from doInBackground.
  • onPostExecute - executed in the main thread to do things like set image views.

References

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.

Clone this wiki locally