Skip to content Skip to sidebar Skip to footer

Onloadfinished() Called Twice

I'm trying to figure out if I'm doing something wrong with respect to Loaders. I'm using the support library, and I have a Fragment which in onCreate() calls initLoader() setting i

Solution 1:

I had a similar problem and the cause was that I had initLoader and restartLoader in my code. Depending on user's action, my query could change, so I needed to restart my loader.

The solution was to use only restartLoader, even in onResume callback method replace initLoader with restartLoader.

Solution 2:

This is a rather old question, but for future readers I have an alternative solution. Basically what I ended up doing was restart the loader if it existed.

publicvoidonActivityCreated(Bundle savedInstanceState) {
...
       if(getLoaderManager().getLoader(Constants.LOADER_ID) == null) {
            getLoaderManager().initLoader(Constants.LOADER_ID, null, this);
        } else {
            getLoaderManager().restartLoader(Constants.LOADER_ID, null, this);
        }

...
}

This solved my issue with that on screen rotate the loader was triggered twice. One thing too note is that this is only needed for me on Android < 6 that I tested. Android 6 seem to not have this issue at all.

Solution 3:

I am experiencing same problem my self, with no good solution. It seems as bug in Android framework, here is similar thread in which proposed solution is to place initLoader() in onResume() - I have tried it and it works, on onLoadFinished() gets called only once: Android: LoaderCallbacks.OnLoadFinished called twice

Solution 4:

See my post at Android: LoaderCallbacks.OnLoadFinished called twice

I had a similar problem when restarting Fragments in a ViewPager. My solution is to remove the Loader once I'm finished with it (at the end of onLoadFinished) by calling

getLoaderManager().destroyLoader(LOADER_ID);

Hope it helps!

Solution 5:

It is looks like framework Loader wrong implementation/bug. 1. look at what I got from Log.v(LOG_TAG, ...) messages from every important method/callback after screen rotation:

...: .initLoader() 100...: onStartLoading()...: onLoadFinished()...: updateUi(); articles size=10...: loadInBackground()...: getInputStream() - HttpRequest...: onLoadFinished()...: updateUi(); articles size=10

2. As you can see everything after 'updateUi()' is no needed there.

Post a Comment for "Onloadfinished() Called Twice"