Android - onLoadFinished not called
Asked Answered
P

5

10

I am facing an issue with Loader.

I have an Activity, which displays list of records retrieved from local DB. When the activity starts, records are automatically loaded via LoaderManager.initLoader() method.

There is also possibility to manually refresh the list via refresh button in ActionBarSherlock. However, after finishing another activity which adds a record to DB, onLoadFinished is not called.

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

@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 issue is that after finishing the other activity, onLoaderCreate is called, but onLoaderFinished is not called.

after some debugging, I've found that SimpleCursorAdapter.deliverResults() is also called, bud ends up on .. if (isReset()) { ..

Am I missing something? How to force the reload of data?

Thank you in advance

Patriciapatrician answered 15/4, 2013 at 12:19 Comment(1)
You may find solution here #31414084 or here #7166863Daye
M
16

I have finally found the solution to this problem thanks to the discussion on

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);
    }
}

In addition as of support library 28 make sure that you don't call initLoader from within Fragment.onCreate(). As the updated documentation states

You typically initialize a Loader within the activity's onCreate() method, or within the fragment's onActivityCreated() method.

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

Machutte answered 4/7, 2013 at 18:16 Comment(1)
Almost the correct solution: as mentioned on groups.google.com/forum/#!topic/android-developers/DbKL6PVyhLI (and I experimented it), the reset condition is !loader.isReset() , i.e., 'You should only restart the loader if the loader is NOT reset.' (written by Etienne). Please update your answer.Letendre
P
4

RaB solution dont work for me

My worked Solution, was always destroy Loader before restart

Loader<Cursor> loader = mLoaderManager.getLoader(mKeyLoader);
if (loader != null)
{
    mLoaderManager.destroyLoader(mKeyLoader);
}
mLoaderManager.restartLoader(mKeyLoader, args, this);
Phew answered 24/3, 2016 at 11:48 Comment(1)
Thanks. I finally found a why to force loader always call onLoadFinished method.Wormeaten
A
2

In addition to RaB's answer, if you are using a custom Loader, make sure that if you call super if you overwrite deliverResult():

@Override
public void deliverResult(D data) {
    super.deliverResult(data); // <--onLoadFinished() will not be called if you don't call this
    ...
}
Arras answered 7/4, 2016 at 8:12 Comment(0)
J
1

fwiw, I had a similar problem from attempting to immediately restart the loader a second time, before the first onLoadFinished was called, resulting in neither being called.

this worked for me:

if( loader == null )
    loader = loaderMngr.initLoader(
        0, null, myLoaderCallbacks
        ); 
else if( loader.isAbandoned() )
    return;
else
    loaderMngr.restartLoader(
        0, null, myLoaderCallbacks
        );    
Johnson answered 11/7, 2014 at 20:22 Comment(0)
M
1

Check the support library.Use this import android.support.v4.app. Don't use android.app.loadermanager.

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

Initialize part

    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);
Macrophysics answered 28/5, 2019 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.