Android - onLoadFinished is not called

I have a problem with the bootloader.

I have an Activity that displays a list of records retrieved from a local database. When the action begins, entries are automatically loaded using the LoaderManager.initLoader () method.

It is also possible to manually update the list using the refresh button in ActionBarSherlock. However, after completing another action that adds a record to the database, onLoadFinished is not called.

I am using SimpleCursorLoader and here is the code snippet from this operation:

@Override
public void onStart() {
   ...
   getSupportLoaderManager().initLoader(0, null, this);
}

@Override
public void onPause() {
   ...
   getSupportLoaderManager().destroyLoader(0);
}

public void refreshRecords() {
   getSupportLoaderManager().restartLoader(0, null, this);
}

@Override
public Loader<Cursor> onCreateLoader(int id, final Bundle args) {
Loader<Cursor> l = new SimpleCursorLoader(this) {
    @Override
    public Cursor loadInBackground() {
        return recordDAO.getCursor();
    }
};
l.forceLoad();
return l;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
   // updateUI
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
}

The problem is that after the completion of another action is called onLoaderCreate, but onLoaderFinishednot called.

after some debugging, I found what is also called the SimpleCursorAdapter.deliverResults()bud ends on.. if (isReset()) { ..

- ? ?

+9
5

https://groups.google.com/forum/#!topic/android-developers/DbKL6PVyhLI

public static <T> void initLoader(final int loaderId, final Bundle args, final LoaderCallbacks<T> callbacks,
        final LoaderManager loaderManager) {
    final Loader<T> loader = loaderManager.getLoader(loaderId);
    if (loader != null && loader.isReset()) {
        loaderManager.restartLoader(loaderId, args, callbacks);
    } else {
        loaderManager.initLoader(loaderId, args, callbacks);
    }
}

, 28, , initLoader Fragment.onCreate().

Loader onCreate() onActivityCreated().

. https://developer.android.com/guide/components/loaders

+16

RaB

,

Loader<Cursor> loader = mLoaderManager.getLoader(mKeyLoader);
if (loader != null)
{
    mLoaderManager.destroyLoader(mKeyLoader);
}
mLoaderManager.restartLoader(mKeyLoader, args, this);
+4

RaB, Loader, , super, deliverResult():

@Override
public void deliverResult(D data) {
    super.deliverResult(data); // <--onLoadFinished() will not be called if you don't call this
    ...
}
+2

fwiw, , , onLoadFinished, .

:

if( loader == null )
    loader = loaderMngr.initLoader(
        0, null, myLoaderCallbacks
        ); 
else if( loader.isAbandoned() )
    return;
else
    loaderMngr.restartLoader(
        0, null, myLoaderCallbacks
        );    
+1

. android.support.v4.app. android.app.loadermanager.

import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;

    LoaderManager mLoaderManager=getSupportLoaderManager(); 

      LoaderManager.LoaderCallbacks<Cursor> mCursorLoaderCallbacks=new LoaderManager.LoaderCallbacks<Cursor>() {
                @Override
                public Loader<Cursor> onCreateLoader(int id, Bundle cursor) {
                      return new CursorLoader(getActivity(), MediaStore.Video.Media.EXTERNAL_CONTENT_URI, COLUMNS_OF_INTEREST, null, null,
                            MediaStore.Video.Media.DATE_ADDED + " DESC");
                }
                @Override
                public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
                }


                @Override
                public void onLoaderReset(Loader<Cursor> loader) {              
               }
            };

  mLoaderManager.initLoader(URL_LOADER_EXTERNAL, null, mCursorLoaderCallbacks);
0

All Articles