page.title=Loading Data in the Background trainingnavtop=true startpage=true @jd:body

Dependencies and prerequisites

Dependencies

Prerequisites

You should also read

A {@link android.support.v4.content.CursorLoader} runs a query against a {@link android.content.ContentProvider} on a background thread and returns a {@link android.database.Cursor} to the main thread.

{@link android.support.v4.content.CursorLoader} has these advantages over alternate ways of running a query:

Query on a background thread
A {@link android.support.v4.content.CursorLoader} query runs asynchronously on a background thread, so it doesn't cause "Application Not Responding" (ANR) errors on the UI thread. {@link android.support.v4.content.CursorLoader} creates and starts the background thread; all you have to do is initialize the loader framework and handle the results of the query.
Automatic re-query
A {@link android.support.v4.content.CursorLoader} automatically runs a new query when the loader framework detects that the data underlying the {@link android.database.Cursor} has changed.
Simple API
The {@link android.support.v4.content.CursorLoader} API provides the query framework and cursor monitoring that you would have to define yourself if you used {@link android.os.AsyncTask}.

A {@link android.support.v4.content.CursorLoader} is limited in that the query must be against a {@link android.net.Uri} and must return a {@link android.database.Cursor}. Because of this, a {@link android.support.v4.content.CursorLoader} can only run a query against a {@link android.content.ContentProvider}.

This class describes how to define and use a {@link android.support.v4.content.CursorLoader}. Examples in this class use the {@link android.support.v4 v4 support library} versions of classes, which support platforms starting with Android 1.6.

Lessons

Setting Up the Loader
Learn how to set up an {@link android.app.Activity} that inherits the necessary classes for running a {@link android.support.v4.content.CursorLoader} and returning results.
Defining and Launching the Query
Learn how to perform a query against a {@link android.content.ContentProvider} using a {@link android.support.v4.content.CursorLoader}.
Handling the Results
Learn how to handle the {@link android.database.Cursor} returned from the query, and how to remove references to the current {@link android.database.Cursor} when the loader framework re-sets the {@link android.support.v4.content.CursorLoader}.