[android-developers] Re: Share Loader's data between multiple fragment

2014-06-04 Thread Doug
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.LoaderD,
  
 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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
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 android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[android-developers] Re: Share Loader's data between multiple fragment

2014-06-04 Thread MathieuB
Like Doug said. That's sort of how i'm doing it and had no problem.

So #2 kind of.

Here's my onLoadFinished :

@Override
public void onLoadFinished(LoaderArrayListTour loader, ArrayListTour 
data) { 

CatalogTabFeatured featuredFragment = (CatalogTabFeatured) 
getSupportFragmentManager().findFragmentByTag(makeFragmentName(mViewPager.getId(),
 
0));

featuredFragment.setData(data);


CatalogTabRecents recentsFragment = (CatalogTabRecents) 
getSupportFragmentManager().findFragmentByTag(makeFragmentName(mViewPager.getId(),
 
1));

recentsFragment.setData(data);

CatalogTabRated ratedFragment = (CatalogTabRated) 
getSupportFragmentManager().findFragmentByTag(makeFragmentName(mViewPager.getId(),
 
2));

ratedFragment.setData(data);

}

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
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 android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [android-developers] Re: Share Loader's data between multiple fragment

2014-06-04 Thread Nathan Barraille
Yeah, I guess I can do that, but if I do I need to write custom code for
retaining the arguments across instance change, which I would have liked
the framework to be able to do for me, as described here:
https://groups.google.com/forum/#!topic/android-developers/bCc5tjOHQ1o


On Wed, Jun 4, 2014 at 8:47 AM, MathieuB blanc...@gmail.com wrote:

 Like Doug said. That's sort of how i'm doing it and had no problem.

 So #2 kind of.

 Here's my onLoadFinished :

 @Override
 public void onLoadFinished(LoaderArrayListTour loader, ArrayListTour
 data) {

 CatalogTabFeatured featuredFragment = (CatalogTabFeatured)
 getSupportFragmentManager().findFragmentByTag(makeFragmentName(mViewPager.getId(),
 0));

 featuredFragment.setData(data);


 CatalogTabRecents recentsFragment = (CatalogTabRecents)
 getSupportFragmentManager().findFragmentByTag(makeFragmentName(mViewPager.getId(),
 1));

 recentsFragment.setData(data);

  CatalogTabRated ratedFragment = (CatalogTabRated)
 getSupportFragmentManager().findFragmentByTag(makeFragmentName(mViewPager.getId(),
 2));

 ratedFragment.setData(data);

 }

 --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.com
 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 a topic in the
 Google Groups Android Developers group.
 To unsubscribe from this topic, visit
 https://groups.google.com/d/topic/android-developers/rFQDqknx8VM/unsubscribe
 .
 To unsubscribe from this group and all its topics, send an email to
 android-developers+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/d/optout.


-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
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 android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [android-developers] Re: Share Loader's data between multiple fragment

2014-06-04 Thread Kostya Vasilyev
Couldn't you abstract the different ways of loading data inside the loader?

...and pass some sort of unique data ID to each fragment to identity the
data, but not the actual way of obtaining it (network, content provider,
etc.).

Then you'd be able to package this ID into each fragment's arguments,
leveraging the framework's persistence mechanism for orientation changes,
activity re-creation, etc...?

-- K


2014-06-04 20:03 GMT+04:00 Nathan Barraille nathan.barrai...@gmail.com:

 Yeah, I guess I can do that, but if I do I need to write custom code for
 retaining the arguments across instance change, which I would have liked
 the framework to be able to do for me, as described here:
 https://groups.google.com/forum/#!topic/android-developers/bCc5tjOHQ1o


 On Wed, Jun 4, 2014 at 8:47 AM, MathieuB blanc...@gmail.com wrote:

 Like Doug said. That's sort of how i'm doing it and had no problem.

 So #2 kind of.

 Here's my onLoadFinished :

 @Override
 public void onLoadFinished(LoaderArrayListTour loader,
 ArrayListTour data) {

 CatalogTabFeatured featuredFragment = (CatalogTabFeatured)
 getSupportFragmentManager().findFragmentByTag(makeFragmentName(mViewPager.getId(),
 0));

 featuredFragment.setData(data);


 CatalogTabRecents recentsFragment = (CatalogTabRecents)
 getSupportFragmentManager().findFragmentByTag(makeFragmentName(mViewPager.getId(),
 1));

 recentsFragment.setData(data);

  CatalogTabRated ratedFragment = (CatalogTabRated)
 getSupportFragmentManager().findFragmentByTag(makeFragmentName(mViewPager.getId(),
 2));

 ratedFragment.setData(data);

 }

 --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.com
 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 a topic in the
 Google Groups Android Developers group.
 To unsubscribe from this topic, visit
 https://groups.google.com/d/topic/android-developers/rFQDqknx8VM/unsubscribe
 .
 To unsubscribe from this group and all its topics, send an email to
 android-developers+unsubscr...@googlegroups.com.

 For more options, visit https://groups.google.com/d/optout.


  --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.com
 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 android-developers+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/d/optout.


-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
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 android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.