I use Cursors
extensively in my app, to load and occasionally write information from and to a database. I have seen that Honeycomb and the Compatibility Package
There are two key benefits to using a CursorLoader
in your app over Activity.managedQuery()
:
AsyncTaskLoader
) so large data queries do not block the UI. This is something the docs recommended you do for yourself when using a plain Cursor
, but now it's done under the hood.CursorLoader
is auto-updating. In addition to performing the initial query, the CursorLoader
registers a ContentObserver
with the dataset you requested and calls forceLoad()
on itself when the data set changes. This results in you getting async callbacks anytime the data changes in order to update the view.Each Loader
instance is also handled through the singular LoaderManager
, so you still don't have to manage the cursor directly, and now the connection can persist even beyond a single Activity
. LoaderManager.initLoader()
and LoaderManager.restartLoader()
allow you to reconnect with an existing Loader
already set up for your query and, in some cases, instantly get the latest data if it is available.
Your Activity
or Fragment
will likely now implement the LoaderManager.Callback
interface. Calling initLoader()
will result in the onCreateLoader()
method where you will construct the query and a new CursorLoader
instance, if necessary. The onLoadFinished()
method will be fired each time new data is available, and will include the latest Cursor
for you to attach to the view or otherwise iterate through.
In addition, there is a pretty good example of all this fitting together on the LoaderManager
class documentation page:
http://developer.android.com/reference/android/app/LoaderManager.html
Hope that Helps!