I would do #2, except don't use setArguments to pass the data in. When the loader callbacks fire, just reach into each fragment instance directly and call a custom method to pass the model in. You might have to check fragment class types and cast to implement this. Personally I would make all the fragments implement an interface such as ModelReceiver which a method receiveModel(Model) so they can get the model object.
Doug On Tuesday, June 3, 2014 3:53:41 PM UTC-7, Nathan Barraille wrote: > > Hello, > I have a problem with the Android framework for which I'm not able to find > any ideal design solution. > I have an activity that contains multiple fragments, all those fragments > need to access the same model object. This model object can be loaded from > a memory cache, ContentProvider or network, depending on a bunch of > parameters. I wanted to use a Loader to load that model object, so that it > doesn't have to be loaded again when the activity is recreated, and it can > be observed if it changes. > All my fragments don't really have a purpose without the model object > loaded, as they don't have any data to display. > Here are the options I considered: > > *#1: Have the activity initialize the loader in its onCreate() method, and > add the fragments to the activity when the model finishes loading: * > *Problem:* It is unsafe to make fragment transactions in a callback > method, because the activity state might be paused, according to > http://developer.android.com/reference/android/app/LoaderManager.LoaderCallbacks.html#onLoadFinished(android.content.Loader<D>, > > D) > > *#2: Have the activity initialize the loader and add the fragments in its > onCreate() method, and call a custom method on the fragment to set the > model* > *Problem:* I cannot use the recommended setArguments() method to set > the data on the fragment, which means that if my fragment is re-created by > the system, it won't have a reference to the model anymore > > *#3: Have the activity initialize the loader and add the fragments in its > onCreate() method, and have the fragments access the model through the > activity* > *Problem*: The fragments won't know whether or not the model is > loaded in the activity, and if I add a method on the fragment for the > activity to let it know, I'll have the same problem as #2 > > *#4: Have all my fragments initialize a loader independently and load the > data on their own.* > *Problem:* That's really inefficient, especially if the model needs > to be loaded from the network > > I'm kind of thinking now that I could do something like #3, and having an > additional call in my fragments' onResume() that checks whether or not the > model is set in the activity to set its state, but that seems hacky... > > Is there a cleaner way of doing this? > > Thanks! > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.

