Click here to Skip to main content
15,889,116 members
Articles / Mobile Apps / Android

Using Cursor Loader in Android

Rate me:
Please Sign up or sign in to vote.
4.76/5 (11 votes)
9 Oct 2014CPOL3 min read 111K   1.9K   18  
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 http://developer.android.com/sdk/compatibility-library.html
2. follow these instructions on setting it up: http://developer.android.com/sdk/compatibility-library.html#SettingUp
3. The activity must extend FragmentActivity. You will probably need to import these classes:
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;

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


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:
http://developer.android.com/guide/components/loaders.html
http://developer.android.com/guide/topics/providers/content-provider-creating.html
 

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.

License

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


Written By
Software Developer 3Pillar Global, Inc
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions