LoaderManager: how to get the right loader from with multiple different loaders?

StackOverflow https://stackoverflow.com/questions/18668586

  •  28-06-2022
  •  | 
  •  

Вопрос

Now I want to create several AsyncTaskLoaders with different types.

One could be:

public Loader<List<Category>> onCreateLoader(int id, final Bundle args) {
    ...
}

The other one could be:

public Loader<BigInteger> onCreateLoader(int id, final Bundle args) {
    ...
}

I read about the post LoaderManager with multiple loaders: how to get the right cursorloader, but simply checking the ID will not fit my case.

Should I just create two sets of implemented methods in the activity to support these two loaders?

Это было полезно?

Решение

You don't have to have your fragment inherit from the Callbacks, it's just convenient in the simplest case. When you have different Loader types, you'll need to create a local Callbacks implementation for each type. Because of type erasure, this is the only way to use multiple loader types without an ugly mess of type checking and casting. The callbacks will be members of your fragment and you'll pass these into the initLoader() methods.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top