What I would like to achieve:
I have two different fragments. I would like them both to show the same data in two forms (in a list and on a map). I would like them to share one Loader (
LoaderManager lm = getActivity().getSupportLoaderManager();
lm.initLoader(0, args, this);
How to re-connect to the same Loader from a different Activity/Fragment than the one it was started in?
You should not reuse
Loaders that are being managed by a
LoaderManager instance across multiple
LoaderManager will start/stop those
Loaders with respect to the
Fragment lifecycle, so there is no way of guaranteeing that those
Loaders will exist once you are in another
From the documentation:
LoaderManager.LoaderCallbacks is a callback interface that lets a client interact with the LoaderManager.
Loaders, in particular CursorLoader, are expected to retain their data after being stopped. This allows applications to keep their data across the activity or fragment's onStop() and onStart() methods, so that when users return to an application, they don't have to wait for the data to reload. You use the LoaderManager.LoaderCallbacks methods when to know when to create a new loader, and to tell the application when it is time to stop using a loader's data.
In other words, it is often the case that your
Loaders will be specific to some Activity (or Fragment). When you have your
Activity implement the
LoaderManager.LoaderCallbacks interface, your Activity is given type
LoaderManager.LoaderCallbacks. Each time you call
initLoader(int ID, Bundle args, LoaderCallbacks<D> callback), the LoaderManager either creates or reuses a
Loader that is specific to some instance of the
LoaderManager.LoaderCallbacks interface (which in this case is an instance of your Activity). This essentially binds your Activity with a Loader, and its callback methods will be called as the loader state changes.
That being said, unless you can find a way to have your two separate Activitys share the same callback methods, I doubt there is a clean way to do this (i.e. having an Activity and a Fragment share the same callbacks sounds like it would be tricky, if not impossible). I wouldn't worry about it too much though. In all of the sample code I have ever seen, I've never seen two Activitys and/or Fragments share the same callback methods. Further, given that
Fragments are both supposed to be designed for reuse, sharing
Loaders in this way just doesn't seem like something that would be encouraged.