When identifying Loaders in your LoaderManager, you use unique ids. I’m asking about how unique those ids have to be.
Does every activity and fragment have its own LoaderManager? Do fragments use the LoaderManager of the Activity they’re attached to? Is there only one LoaderManager that the application owns?
Bonus points if you can tell me how it’s possible to change which LoaderManager you’re using. If I want every fragment in my Activity to use the same LoaderManager (some of them are pulling the same data and sharing Loaders would be nice), is that possible?
I’m currently porting my application to the android compatibility package (mainly for CursorLoader and Fragments). I’m currently trying to share a CursorLoader between two fragments to spare a query to my ContentProvider. Welcome to my world! 😉
A simple use case:
− DummyActivity extends FragmentActivity / Log.d(Constants.LOGTAG, “DummyActivity.onCreate ” + getSupportLoaderManager().toString());
− DataFragment extends Fragment implements LoaderManager.LoaderCallbacks / Log.d(Constants.LOGTAG, “DataFragment.onCreate ” + getLoaderManager().toString());
− ReportFragment extends Fragment implements LoaderManager.LoaderCallbacks / Log.d(Constants.LOGTAG, “ReportFragment.onCreate ” + getLoaderManager().toString());
DummyActivity instanciates the DataFragment and the later instanciates the ReportFragment. The logcat output shows differents addresses for each LoaderManager. As a first conclusion, each Fragment seems to have a proper LoaderManager…
I will continue and update if I can answer to your (our 😉 ) question. If you have made any progress, please share your valuable knowledge.
Update:
My assumption is that the loader ids are only associated with a local scope of a LoaderManager for a specific fragment to enable several local loaders to be associated with the fragment (so you can return a different loader in onCreateLoader based on the id int arg and the initLoader calls).
So far I managed to “reuse” a Loader (… or not):
− First, I have enabled LoaderManager debugging with
getSupportLoaderManager().enableDebugLogging(true);in the DummyActivityonCreatemethod.− Then I have called
getActivity().getSupportLoaderManager().initLoader(78, null, this);from theonCreatemethods of both DataFragment and ReportFragment.− DataFragment exposes the CursorLoader created by the
onCreateLoadermethod via a setter on a mCursorLoader private member.− The ReportFragment
onCreateLoaderreturns the DataFragment CursorLoader (after retrieving the Fragment withfindFragmentByTag).The filtered (and slightly obfuscated) logcat output:
The two fragments are added from the DummyActivity
onCreatemethod (different from the described use case, but that changes nothing to the issue we are working on). Unfortunately, the loader is reassigned to the latest fragment calling it (here ReportFragment)… and DataFragment.onLoadFinished is never called. As a consequence, the ReportFragment looks good but the DataFragment is not up-to-date since the update is called from theonLoadFinishedof this class.I assume that there is an underlying unregister call then a register call on the CursorLoader.
To be continued…