In Honeycomb the Loader APIs were introduced as the proper way to provide data to an application by doing the heavy lifting on a background thread. In my application I’m working to replace all my Cursors with Loaders that return Cursors. Since Cursor.requery() is depreciated now, it is recommended to just call restartLoader and allow the work to again be done on a background thread and then changeCursor when it returns in onLoadFinished.
All of this works wonderfully except that the ListView doesn’t maintain it’s scroll position when I want to requery the data, using Cursor.requery() this used to work because it was the same Cursor instance with updated data.
How can I refresh my Loader without loosing the scroll position?
The
Loaderclass by itself doesn’t know when the dataset changes and is implementation specific toCursorLoadercurrently. Now inside my application the data I needed to refresh is inside a local SQLite database (no ContentProvider) so I had to modify theCursorLoaderto use my local database instead, as Dianne Hackborne mentioned on the developer group.Unfortunately just returning a Cursor from loadInBackground doesn’t allow the
ContentObserverto properly notify the Loader when the data changes, this is becauseAbstractCursoronly callsnotifyChanged()whenrequery()is called on the Cursor.Since the
ContentObserverwill never notify theLoaderthat the data has changed, I ended up callingLoader.onContentChanged()myself as needed. At this point it everything seems to be working but I’ll update this answer if any significant issues arrise.