Intoduction● Asynchronously load data in an activity or fragment.● Automatically reconnect to the last their cursor when being recreated after a configuration change (like screen flips). Thus, they dont need to re-query their data.● Introduced in Honeycomb, exist on Android Support Package v-4.
Involved Classes and Interfaces● LoaderManager● LoaderManager.LoaderCallbacks● Loader● AsyncTaskLoader● CursorLoader
Common Approach...● LoaderManager to initialize a Loader. There is only one LoaderManager per activity or fragment, but a LoaderManager can have multiple loaders.● LoaderManager.LoaderCallbacks: a callback interface for a client to interact with the LoaderManager.● Loader: an abstract class that performs asynchronous loading of data. This is the base class for a loader.
...Common Approach● AsyncTaskLoader: abstract loader that provides an AsyncTask to do the work.● A CursorLoader used to load data which backed by a ContentProvider. Alternatively, implement a subclass of Loader or AsyncTaskLoader to load data from some other source.● A SimpleCursorAdapter used to display the loader’s data.
Implementation● Starting a Loader – getLoaderManager().initLoader(0, null, this); – Within Activity via onCreate() method. – Within Fragment via onActivityCreated() method.● Restarting a Loader – getLoaderManager().restartLoader(0, null, this);
Using the LoaderManager CallbacksLoaderManager.LoaderCallbacks includes thesemethods: – onCreateLoader() — Instantiate and return a new Loader for the given ID. – onLoadFinished() — Called when a previously created Loader has finished its load. – onLoaderReset() — Called when a previously created Loader is being reset, thus making its data.