Search code examples
androidandroid-contentproviderandroid-loadermanager

LoaderManager.onLoadFinished is called even when the specific query doesn't match the update


I have a Android app which has a Content Provider which I update frequently. To load the lists of the said data, I'm using a LoaderManager.

On the detail page, I also use a LoaderManager, but I create the CursorLoader like this:

return new CursorLoader(getActivity(), DataProvider.POST_URI, projection, "_id = ?", new String[]{String.valueOf(post_id)}, null);

As you can see, I'm only getting a single item from the table. When the row is updated the the onLoadFinished callback is updated, but, also, when other rows are added/modified to the table. It wouldn't be much of a hassle, but sometimes, it causes the whole list to get blank and redrawn, which can be frustrating for the user when it happens multiple times.

Do you know how could I avoid this behavior?


Solution

  • The reloading is taking place because you are associating this cursor loader with the DataProvider.POST_URI which I assume is the one you use for your posts table. Whenever there is a change in any of the elements in this table, all the cursors with that Uri will be notified. If you want to receive notification of a single item you should implement a new Uri scheme to match just that one item. Have a look at the documentation for the UriMatcher class for an example. You should do something similar to the PEOPLE and PEOPLE_ID cases.

    EDIT

    You can find a good tutorial on ContentProvider here. Section 9.4 in special should be helpful in correctly implementing the ContentProvider.