Android - onLoadFinished not called

前端 未结 5 2229
谎友^
谎友^ 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  void initLoader(final int loaderId, final Bundle args, final LoaderCallbacks callbacks,
            final LoaderManager loaderManager) {
        final Loader 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

提交回复
热议问题