Android FragmentPagerAdapter with LoaderManager

I have a FragmentActivity with ViewPager and FragmentPagerAdapter.

Each fragment in the ViewPager must load data from the web service. I wrote LoaderManager / custom Loaders to make webservice calls, parse XML, etc., and they work just fine.

There are two possible options for managing downloads and fragments.

1) The main action for controlling loading on behalf of fragments.

A fragment performs a callback to the main FragmentActivity function with a data request. The action checks if the bootloader is started for this data if it does not create it and does not load.

In this case, how to tell the fragment that the data has been loaded and ready for drawing. I use custom loaders that are not simple cursors, so the fragment fills its text objects, etc. From an instance of an object, so it must be specified when the objects were filled.

It seems that the FragmentPagerAdapter allows you to select tags or identifiers for fragments when they are created in getItem. How can I find a fragment from FragmentActivity and tell it to draw its data?

2) Each fragment controls its own loading.

A fragment initializes its own loader, each fragment has its own onCreateLoader / onLoaderFinish, etc.

The problem I ran into is that the user pages from this page, while the loader is working with the fragment, seem to be destroyed (sometimes). As a result, onLoaderFinished is not called, and the fragment cannot determine the main activity that it completed - the main activity is to monitor the progress indicator (setProgressBarIndeterminateVisibility) in the action bar.

So, what is the best design template when you have a FragmentPagerAdapter, a ViewPager with multiple pages that need custom loaders?

Another issue that I encountered is using getSupportLoaderManager to check if any bootloaders are running. I call this from onLoadFinished. If no downloaders work, I can hide the progress indicator. However, hasRunningLoaders sometimes returns true, although all bootloaders are complete.

LoaderManager lm = getSupportLoaderManager(); // If the loader is not already running, start it... if (! lm.hasRunningLoaders()) { setProgressBarIndeterminateVisibility(Boolean.FALSE); } 

Thank you very much for any advice or pointing me towards some decent samples.

That Martin.

+4
source share
2 answers

As for design 1, I did something like this, maintaining a reference variable for the fragments I create. In your get1tem FragmentPagerAdapter method, you can assign the created fragment to this variable before returning it. This will allow access to your fragments from the main action.

0
source

I'm not sure that this is absolutely correct, but I remember that when your fragment is destroyed, when the loader receives data in the background, the onLoaderReset method is onLoaderReset (instead of onLoaderFinished ). This will allow you to reset your progress indicator accordingly.

0
source

Source: https://habr.com/ru/post/1392046/


All Articles