Multiple AsyncTaskLoader in one action - should each bootloader be initialized in onCreate?

I have a FragmentActivity with a ViewPager in it - this ViewPager contains three ListViews - each of them has its own unique adapters (and unique data sets).

I would like to use AsyncTaskLoader to populate these adapters, but ONLY when the selected view is selected in the ViewPager.

Do I need to initialize the bootloader in the onCreate method? (below code)

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_foo); getSupportLoaderManager().initLoader(0, null, this); getSupportLoaderManager().initLoader(1, null, this); getSupportLoaderManager().initLoader(2, null, this); } 

Or can you normally call the initLoader (...) call interactively, for example, the listening result for OnPageChangeListener.onPageSelected?

 mPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int newPage) { getSupportLoaderManager().initLoader(newPage, null, this); } ... } 

I want to avoid the need to run potentially 3 temporary bootloaders in front if the user can only look at the first view in the pager.

EDIT:. Or would it be better if the ViewPager used a fragment for each view, and each fragment would be responsible for managing its own loader?

+6
source share
2 answers

I don’t remember the details, but at least with the compatibility library, if you do not call initLoader() from onCreate() , the bootloader life cycle is confused, so you can call them from onCreate() (it can be fixed in the latest version, not verified). IIRC fragments just use LoaderManager activity, so loaders are always action driven. Using fragments tends to make your code cleaner, so go to fragments if possible.

+1
source

Since you are using Fragments, I would suggest using fragments in your view player. I do and it works much better. Then you really initialize one loader per fragment. If these fragments are interconnected, you can define an abstract extension and put the general initialization logic there, then you can expand this to create a specific fragment

In your code above, I found that initializing each subsequent bootloader cancels the previous one, even the identifiers are different

+1
source

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


All Articles