Multiple loaders in same activity

android, java

Solution

The correct answer is as per @dymmeh's comment, i.e. not for the `Activity` to implement two `LoaderCallbacks` interfaces but for the activity to contain two `LoaderCallbacks` implementations. By way of example: initialise your `LoaderCallbacks` fields in your activity...

private LoaderCallbacks<GetSyncListDataResult> dataResultLoaderListener
  = new LoaderCallbacks<GetSyncListDataResult>() { ...methods here... };

private LoaderCallbacks<ErrorResult> errorResultLoaderListener
  = new LoaderCallbacks<ErrorResult>() { ...methods here... };

... and declare your loader ids...

private static final int DATA_RESULT_LOADER_ID = 1;
private static final int ERROR_RESULT_LOADER_ID = 2;

... and then initialise your loaders...

getLoaderManager().initLoader(DATA_RESULT_LOADER_ID, dataResultBundle, dataResultLoaderListener);
getLoaderManager().initLoader(ERROR_RESULT_LOADER_ID, errorResultBundle, errorResultLoaderListener);

... Done!

Problem

I have two custom built loaders inherited from `AsyncTaskLoader` which I would like to use in my activity. Each of them returns result of different type. To use my activity for a callback I must implement two interfaces: ``` implements LoaderCallbacks<GetSyncListDataResult>, LoaderCallbacks<ErrorResult> ``` However, trying to implement required methods in the same class I end up with duplicate method error and erasure(???) error: ``` // Methods for the first loader public Loader<GetSyncListDataResult> onCreateLoader(int ID, Bundle bundle) ... public void onLoaderReset(Loader<GetSyncListDataResult> loader) ... public void onLoadFinished(Loader<GetSyncListDataResult> loader, GetSyncListDataResult result) ... // Methods for the second loader public Loader<ErrorResult> onCreateLoader(int ID, Bundle bundle) ... public void onLoaderReset(Loader<ErrorResult> loader) ... public void onLoadFinished(Loader<ErrorResult> loader, ErrorResult result) ... ``` Obviously, the methods are clashing and I need an easy way how to resolve this. What would be the proper way of resolving this?

Original source