2

I want to use different AsyncTaskLoaders (different in their return type) in my Activity, what's the best way to implement the callback methods?

This won't work:

public class MyActivity extends Activity implements LoaderManager.LoaderCallbacks<MyPojo>, LoaderManager.LoaderCallbacks<MyOtherPojo>

Eclipse says

The interface LoaderCallbacks cannot be implemented more than once with different arguments

So what do I do? My idea is to make the Activity

implements LoaderManager.LoaderCallbacks<Object>

then check in the callback methods what type of object it is but that doesn't seem too elegant. Is there a better way?

fweigl
  • 21,278
  • 20
  • 114
  • 205

1 Answers1

9

What about creating an inner class for each callback?

public class MyClass extends Activity {

  private class Callback1 implements LoaderManager.LoaderCallbacks<MyPojo> {
    ...
  }
  private class Callback2 implements LoaderManager.LoaderCallbacks<MyOtherPojo> {
    ...
  }
}
SimonSays
  • 10,867
  • 7
  • 44
  • 59
  • But how do u call init loader in this case ? – Amey Jahagirdar Dec 21 '16 at 05:49
  • @AmeyJahagirdar With a new Instance of the inner class: `getLoaderManager().initLoader(LOADER_MYPOJO_ID, null, new Callback1());` Or if several Loaders should use the same callback (the loaders need to be registrered with distinct IDs so onCreateLoader and onLoadFinished can distinguish where the callback was called from) you can have the Activity have a callback member and use this for initLoader(): `private final LoaderCallbacks reusableCallback1 = new LoaderCallbacks { ... };` – Father Stack Jan 11 '17 at 11:19