Loader
suggest changeVersions
This topic was documented against Android 3.0 through 7.1.
Introduction
A loader is a good choice when you load data in the background from onCreate and want to avoid leaking the old activity or starting a second load after rotation. If you execute an AsyncTask in onCreate and rotate the screen, the activity is recreated and starts another AsyncTask, so two tasks can run in parallel. A loader continues the background work that was already started.
Parameters
| Class | Description |
|---|---|
| LoaderManager | An abstract class associated with an Activity or Fragment for managing one or more Loader instances. |
| LoaderManager.LoaderCallbacks | A callback interface for a client to interact with the LoaderManager. |
| Loader | An abstract class that performs asynchronous loading of data. |
| AsyncTaskLoader | Abstract loader that provides an AsyncTask to do the work. |
| CursorLoader | A subclass of AsyncTaskLoader that queries the ContentResolver and returns a Cursor. |
Remarks
Introduced in Android 3.0, loaders make it easy to asynchronously load data in an activity or fragment. Loaders have these characteristics:
- They are available to every Activity and Fragment.
- They provide asynchronous loading of data.
- They monitor the source of their data and deliver new results when the content changes.
- They automatically reconnect to the last loader's cursor when being recreated after a configuration change. Thus, they don't need to re-query their data.
When not to use Loaders
Avoid loaders when the background work has to run to completion. Android destroys loaders with the activities and fragments they belong to. For work that must finish after that, use a service.