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
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'sonActivityCreated()
method.
see https://developer.android.com/guide/components/loaders