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

1.1k views Asked by At

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?

1

There are 1 answers

1
Krylez On BEST ANSWER

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.