Chapter 14. Automatic Background Execution with Loaders

The Loader framework offers a robust way to run asynchronous operations with content providers or other data sources. The framework can load data asynchronously and deliver it to your application when content changes or is added to the data source. The Loader framework was added to the Android platform in Honeycomb (API level 11), along with the compatibility package.

You can connect to the Loader framework from an Activity or a Fragment. When you create the Loader object, you request a loader that manages your connection with the data source. (Note that I’m using uppercase for the framework and lowercase for the object you connect.)

When you connect with a content provider, the framework contains a loader named CursorLoader that you can hook into. For other data sources, you can code up a custom loader. For any loader type, you have to define three callbacks: one that creates a new loader, one that runs whenever the loader delivers new data, and one that runs when the loader is reset—i.e., the loader stops delivering data.

Some of the features offered by the Loader framework are:

Asynchronous data management
The loader reacts in the background to the data source and triggers a callback in your app when the data source has new data.
Lifecycle management
When your Activity or Fragment stops, its loader stops as well. Furthermore, loaders that are running in the background continue to do their work after configuration changes, ...

Get Efficient Android Threading 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.