Search code examples
androidandroid-contentproviderandroid-cursorloader

Do apps that won't share need Content Provider?


since i noticed the class SimpleCursorAdapter is deprecated and I should now take advantage of the new Loader APIs, which I really like, however when I tried to do so, I found out that CursorLoader works only with ContentProvider.

Now my question is, do I really need a content provider? Even the official guide says:

You don't need to develop your own provider if you don't intend to share your data with other applications. However, you do need your own provider to provide custom search suggestions in your own application. You also need your own provider if you want to copy and paste complex data or files from your application to other applications.

And I think I dont need any on this + it therefore creates unnecessary complexity. So .. what should I do, hack my own CursorLoader to work only with my database like this (CursorLoader usage without ContentProvider), which, honestly i dont really like, or should i just suck it up and conform to making a provider?

Thanks!


Solution

  • You can write a Loader (or use CommonsWare's SQLiteCursorLoader) to query your SQLiteDatabase directly instead. The documentation is correct in that you don't really need a ContentProvider if your app only requires simple access to local data (as opposed to sharing that data with different processes/applications).

    That said, the ContentProvider does offer a few benefits. For example, you need one to implement a SyncAdapter or a search interface with the SearchManager. I try to incorporate these into my applications so I find myself implementing ContentProviders all the time. The ContentResolver also provides an easy means of providing global notifications to the underlying data source when changes are made. For example, the CursorLoader will register a ContentObserver on its Cursor, causing the Cursor to receive a notification when you call ContentResolver#notifyChange(Uri uri, ContentObserver observer) on the given Uri. If you were to load data directly from your SQLiteDatabase instead, setting this up would require a bit more work.