Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Using Cursor Loader in Android

, 9 Oct 2014 CPOL
The article helps in understanding the use of Cursor Loader in Android.
Cursor Loader class is introduced in 3.0 (API Level 11, Honeycomb)
It queries ContentResolver and returns a Cursor. It queries cursor in background thread so that it does not block 
application's UI.
Loader persist the data fetched to avoid repetitive fetch operations for simple activity refresh event like orientation 
change, keyboard open etc. 
Internally loaders use async tasks to perform the data load. So there is no performance gain that one would notice when 
compared to async tasks, provide that the async tasks are designed and developed properly.
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 avoiding the 
need to re-query their data.
They are available as a part of the compatibility library. So developers can use it in applications that run on android 
build previous to HoneyComb.
CursorLoader auto updates, so no need to requery the cursor.
CursorLoader will handle the life cycle of the cursor for you. When using CursorLoader, you should never call close() on 
the cursor.
Use CursorLoader instead of Activity.managedQuery or Activity.startManagingCursor starting in android 3.0. You should 
use it if you want your app to be compatible with android 3.0 or greater.

Using cursor loaders with android < 3.0:
1. you need to use the compatibility library
2. follow these instructions on setting it up:
3. The activity must extend FragmentActivity. You will probably need to import these classes:

Iif you're using SimpleCursorAdapter, make sure you replace this line:
import android.widget.SimpleCursorAdapter;
with this:

Steps to use cursor loaders:
1. The activity needs to implement LoaderManager.LoaderCallbacks:
public class MyActivity implements LoaderManager.LoaderCallbacks<Cursor> {

2. Initialize the loader:
android < 3.0:
getSupportLoaderManager().initLoader(0, null, this);

android >= 3.0:
getLoaderManager().initLoader(0, null, this);

3. Implement the loader callback methods
onCreateLoader: This is where you create the cursor
onLoadFinished : This is called once the cursor's data is finished loading, and you can start using the cursor.
onLoaderReset : This is called when a loader is being reset in order to create a new cursor to query different data. Here 
you need to make sure you're done using the cursor.  This is called when the last Cursor provided to onLoadFinished()
above is about to be closed.  We need to make sure we are no longer using it.

An application that uses loaders typically includes the following:
An Activity or Fragment.
An instance of the LoaderManager.
A CursorLoader to load data backed by a ContentProvider. Alternatively, you can implement your own subclass of Loader or AsyncTaskLoader to load data from some other source.
An implementation for LoaderManager.LoaderCallbacks. This is where you create new loaders and manage your references to existing loaders.
A way of displaying the loader's data, such as a SimpleCursorAdapter.
A data source, such as a ContentProvider, when using a CursorLoader.

Help Documents:

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Khushboo Kaur
Software Developer 3Pillar Global, Inc
India India
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.1 | Last Updated 9 Oct 2014
Article Copyright 2013 by Khushboo Kaur
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid