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 itself as the LoaderCallbacks, however on a rotation it is receiving the result twice in onLoadFinished(), once as a result of calling init (and it already having the data), and once as a result of FragmentActivity looping through all Loaders in onStart() and delivering the result since it already has the data.

If I only call init once (on first launch of the Fragment), it doesn't set itself as the callback for the Loader so it doesn't receive a call to onLoadFinished at all. It seems as though onLoadFinished should only be called once since some expensive things may be done in onLoadFinished() (such as clearing list adapters, etc.), so I'm just trying to figure out if this is a bug or if I am just calling init at the wrong time or something else.

Anyone have any insight to this issue?

Answers


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.


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.

public void onActivityCreated(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.


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


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!


Need Your Help

what could be the correct regex for reading first few bytes in fixed size fron file and reading remaining bytes from first regex's (.*) part in ruby?

ruby regex file byte

I am reading one file using ruby in which I have written regexp in my code as below assuming it will read first 4 bytes from file, next four bytes, next four bytes, rest of the bytes in file except...

HTML - Put SELECT tag content into INPUT type = "text"

javascript html dom html-form html-select

I have a form in a webpage where I would like to put the selected item in a drop down list into a testbox. The code I have till now is the following: