AsyncTask

Very often, your application has to deal with the sequence that was shown in Listing 5–2:

  • Event is received in UI thread
  • Operation is to be executed in non-UI thread in response to event
  • UI needs to be updated with result of operation

To simplify this common pattern, Android defines the AsyncTask class in Android 1.5 and above. The AsyncTask class allows your application to easily perform a background operation and publish the result in the UI thread. Threads, Runnables, and other related objects are hidden from you for simplicity. Listing 5–5 shows how you would implement the sequence from Listing 5–2 using the AsyncTask class.

Listing 5–5. Using AsyncTask

public void onClick (View v) {     // AsyncTask<Params, Progress, Result> ...

Get Pro Android Apps Performance Optimization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.