Search code examples
androidandroid-loadermanagerandroid-cursorloader

Android Cursor Loader only works once


I'm using cursor loaders to load data from my content providers and not always getting results. On my initial call to the content loader it returns the correct results:

    getSupportLoaderManager().restartLoader(
                LOADER_LOCATION_INFO, null, this);

However when I run this query again (using the same code above) I do not get any results. The loader callback is not called.

public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
    switch(arg0){
    case (LOADER_LOCATION_INFO):
        return new CursorLoader(this, 
                               //my query

    }
    return null;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor arg2) {
    switch (loader.getId()) {
    case (MY_CASE):
        doSomething(arg2);
        break;
    }   
}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
    // TODO Auto-generated method stub

};

I don't know if it matters, but I am also making calls to the loaderManager from a fragment inside this activity as well. Those queries work fine. What would cause this? Any help would be appreciated.


Solution

  • My problem was with the Loader IDs. I have LoaderCallbacks in several fragments and the parent activity. I was declaring the IDs in each fragment/activity using static final ints. I was using the same IDs. I moved all the IDs to the parent activity and made them unique. This fixed the issue.