16

When creating an Android application using Loaders, should every activity and fragment have its own LoaderManager? Or should there only be one LoaderManager that the application owns? And lastly, are the "unique IDs" that are used to identify specific LoaderManagers visible outside of the class?

Specifically, I'm having trouble deciding which classes in my application should implement the LoaderCallback<Cursor> methods (i.e. should each fragment implement these callbacks, or should I have one fragment implement the callbacks and query the results, sending them to other fragments/activities as necessary)?

Thanks in advance to anyone who can help me out! I couldn't find too much information about this online.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250

1 Answers1

13

LoaderManger's are managed and owned by the activity. You can create the actual loaders in your fragments or the activity, they will be manged by the same LoaderManager. Unique ID's are to identify different loaders you might have in the same activitiy. For example ID=0 -> FooLoader, ID=1 -> BarLoader, etc.

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • 4
    Hey Nikolay, not sure if you'll ever see this... but one question. If `Fragment`s are meant to be designed for re-use (i.e. you don't want to design the `Fragment` for a single specific parent `Activity`), does that mean you don't want to have a specific `Activity` responsible for managing a `Loader` in a `Fragment`? Is it better practice to manage `Loader`s in the `Fragment` so they can be attached to multiple `Activity`s during the application's life cycle? – Alex Lockwood Jan 29 '12 at 17:56
  • 2
    The `Activity` doesn't really care about the actual loaders, is just makes sure they are created, initialized, etc. properly. You should implement the `LoaderCallback>`'s in the place you are actually using the data. Most probably your `Fragment` classes. – Nikolay Elenkov Jan 30 '12 at 03:32
  • @NikolayElenkov You say that LoaderManagers are managed and owned by Activity. Does it mean, that there is no possibility to use the same LoaderManager in multiple Activities/Fragments? Or different LoaderManager, but the same Loader. Maybe one can assign it to application context instead of Activity, and just implement LoaderCallbacks? – Michał Klimczak Apr 06 '12 at 14:43
  • 2
    You can't really change the `LoaderManager`, just loaders. You can reuse loaders if you make them independent from a particular fragment/activity. You shouldn't assign stuff to application context, since that means it will never be GC-ed (as long as your process is alive). – Nikolay Elenkov Apr 08 '12 at 07:05
  • @AlexLockwood Thanks a lot, very helpful! And Nikolay too! – user1422551 Aug 16 '12 at 01:17
  • 1
    doesn't it mean that you should have a global IDs manager to hold all of loaders, since multiple instances of fragments can be used on multiple/single activities? – android developer Mar 26 '14 at 20:01
  • @androiddeveloper just the point that got me to this question it seems to me that if you have an use case in which you have an activity that contains a viewpager with serveral fragments that in turn use loaders to load data then i have to manage the ids in those fragments to be distinct otherwise it lead me to issues – forcewill Dec 17 '14 at 12:08
  • @forcewill Yes, I assume it can be a bit annoying to manage. Maybe you could put a global singleton counter that increases for every new instance of this class, and when a fragment/activity is about to be destroyed (because of screen rotation, for example), you could store the id of this instance, so that you could manage it again later. – android developer Dec 18 '14 at 21:16