AsynLoaderTask and ViewPager
Asked Answered
C

1

3

I have a ViewPager with a FragmentStatePagerAdapter. In the onViewCreated method of the ViewPager fragments I call the initLoader method of the LoadManager to start an AsyncTaskLoader like this

    public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        list = (ListView) view.findViewById(R.id.list);
        list.setEmptyView(view.findViewById(R.id.empty));
        getLoaderManager().initLoader(0, null, this).forceLoad();
    }

The fragment of course implements LoaderCallbacks, find below the implementation of the relevant methods:

    @Override
public Loader<List<String>> onCreateLoader(int id, Bundle args) {

    logger.debug("Created loader");

    return new AsyncTaskLoader<List<String>>(getActivity()) {

        @Override
        public List<String> loadInBackground() {
             return getResults();
        }

    };
}

@Override
public void onLoadFinished(Loader<List<String>> loader,
        List<String> data) {
    logger.debug("loader finished");
    if (data != null) {
        adapter = new CustomListAdapter(getActivity());
        list.setAdapter(adapter);
        adapter.addAll(data);
    }
}

@Override
public void onLoaderReset(Loader<List<String>> loader) {

}

The problem I am having is that for the first page everything works as expected, but for the subsequent pages, I see that the onCreateLoader call is done, the loadInBackground method of the AsyncTaskLoader is called, but the onLoadFinished is not invoked and therefore now results are delivered.

I am using the Android support library.

Does anyone know what am I doing wrong?

Cl answered 26/7, 2013 at 15:14 Comment(1)
Bug started with revision 13, this help: https://mcmap.net/q/1893252/-onresume-not-called-on-viewpager-fragment-when-using-custom-loaderRequite
C
0

So I just ran into this issue, and it seems you are doing it exactly as I did. Looks like if your loaders are all using the same id (in your example 0) they will restart and stomp all over each other while paging if one page load takes longer than another. Hope this can still be relevant to someone that run into this!

Crossstaff answered 11/7, 2016 at 3:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.