Android - onLoadFinished not called

前端 未结 5 2228
谎友^
谎友^ 2021-02-14 12:03

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

相关标签:
5条回答
  • 2021-02-14 12:40

    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

    0 讨论(0)
  • 2021-02-14 12:44

    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);
    
    0 讨论(0)
  • 2021-02-14 12:48

    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);
    
    0 讨论(0)
  • 2021-02-14 12:59

    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
        ...
    }
    
    0 讨论(0)
  • 2021-02-14 12:59

    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
            );    
    
    0 讨论(0)
提交回复
热议问题