The two implementations you mention in your post both offer all of the benefits of the CursorLoader except the ability to receive notifications when the underlying content changes.
I’ve been looking into this a lot recently and I can confidently tell you that the Android API currently does not provide a means of doing this with only a raw SQLiteDatabase (it only provides the ContentResolver#notifyChange() and Cursor#setNotificationUri() methods, which are used to notify all Cursors registered under a certain notification Uri).
That said, your options right now are to:
-
Implement an observer yourself that is capable of receiving notifications from the
SQLiteDatabasewhen the content changes, and is somehow able to relay these notifications to all existingLoaders in your application. I wrote a pretty extensive blog post on how to implementLoaders that might come in handy if you wish to take on this challenge. Or… -
Use Mark Murphy’s
LoaderExlibrary and only make database modifications using theAsyncTaskoperations his library provides. Note that the reason why his tasks refresh theLoaderis because they callonContentChangedon theLoaderimmediately after the insertion/update/delete is performed, effectively telling theLoaderthat the content has changed and that it should refresh its data. -
Just use a
ContentProviderwith aCursorLoaderand you can use theContentResolver#notifyChange()method to notify theCursorLoaderthat a content change has occurred.
I’m trying to figure out a better solution, and I’ll report back in the future if I ever find/implement one, but for now these will have to do.