What is the scope of LoaderManager?
Asked Answered
A

1

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.

Alishiaalisia answered 25/12, 2011 at 17:36 Comment(0)
F
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.

Formenti answered 26/12, 2011 at 2:31 Comment(8)
Hey Nikolay, not sure if you'll ever see this... but one question. If Fragments 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 Loaders in the Fragment so they can be attached to multiple Activitys during the application's life cycle?Alishiaalisia
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.Formenti
@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?Procurable
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).Formenti
@AlexLockwood Thanks a lot, very helpful! And Nikolay too!Nanna
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?Clearly
@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 issuesKrugersdorp
@Krugersdorp 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.Clearly

© 2022 - 2024 — McMap. All rights reserved.