Tuesday 13 September 2011

Cursor management in Honeycomb


If you are targeting your application for honeycomb, then some APIs related to cursor management are deprecated and your application won't work smoothly in honeycomb, even though it will work well in other versions of Android. In Honeycomb, cursor management is more tightly coupled and Some of the APIs that are deprecated are

- startManagingCursor()
- stopManagingCursor()
- managedQuery()
- reQuery()


If you are using any of these methods in HoneyComb, then you will get an exception, like

java.lang.IllegalStateException: trying to requery an already closed cursor

If you are using managedQuery() in lower versions of Android(2.3,2.2) as,

cursor = context.managedQuery(android.provider.Browser.BOOKMARKS_URI, projection, null, null,
null);


then in Honeycomb you need to modify as,

CursorLoader cursorLoader = new CursorLoader(context, android.provider.Browser.BOOKMARKS_URI, projection, null, null,
null);
cursor = cursorLoader .loadInBackground();

Other modification is , you don't need to call reQuery in honeyComb. You have LoaderManager callbacks are there in honeyComb. That will be called if there is any change in the underlying data.

For implementing LoaderManager callbacks

- First implement the interface in your class as LoaderManager.LoaderCallbacks
- in onCreate you need to initialize the loader as
getLoaderManager().initLoader(0, null, this);
- for reQuery, you can use, getLoaderManager().restartLoader(0, null, this);
- Then you need to override three methods in it as,
- onCreateLoader()
- onLoadFinished()
- onLoaderReset()

No comments:

Post a Comment