0

I have a ViewPager with 3 Fragments in there. Each Fragment uses a AsyncTaskLoader to get there Data from a Database. Now i want to restart the loader of Fragment A from Fragment C but if i call

getLoaderManager.getLoader( "LOADER_A_ID" )

the result is null. I read each LoaderManager has one Activity/Fragment and so has each Fragment a own LoaderManager. Has anybody a good idea to solve my problem?

I would implement a GlobalLoaderManager which is a Singleton-Pattern and hold Fragment. The ViewPager reused his Fragments so i have only one instance of each Pager Fragment in this Manager.

Happo
  • 1,375
  • 3
  • 16
  • 34
  • Why do you need to restart a loader of a fragment that isn't even visible from another fragment? You could implement a callback and let the activity restart the loader. – user Aug 28 '12 at 12:02
  • Each Fragment shows individual data but they are connected at the db model bottom. For example: Fragment A shows Recipes with a State cookable, Fragment B shows Ingredients with amounts. Now i change the amount of one ingredient and want to tell Fragment A to reload his recipes to refresh the cookable state. – Happo Aug 28 '12 at 13:20
  • One option, I think, is to set a `OnPageChangeListener` on the `ViewPager` and in the `onPageSelected` you'll use the position(if the page position is the right one(like the first fragment)) + a boolean flag. The boolean flag(from the activity) will be set by the fragments that modify the data(when that happens). Then in the `onPageSelected` you'll use the `FragmentManager` to get the `Fragment` for that page with : `getSupportFragmentManager().findFragmentByTag("android:"+R.id.viewpager_id+":"+position);`. Then call a method to restart the loader in that `Fragment`. – user Aug 29 '12 at 12:24
  • Sounds nice for me. I think i test this and give you a comment about the result. Thank you. – Happo Aug 29 '12 at 12:33
  • I've made a small adjustment to some old code to show you how you might do what I said above. You can find it here https://gist.github.com/3511928 – user Aug 29 '12 at 12:44
  • Wow looks really nice. Thank you for the fast sample. – Happo Aug 29 '12 at 13:57

1 Answers1

2

The answer is the post from Luksprog. Thank you for your help.

I've made a small adjustment to some old code to show you how you might do what I said above. You can find it here gist.github.com/3511928 – Luksprog

Abhishek
  • 1,654
  • 2
  • 18
  • 31
Happo
  • 1,375
  • 3
  • 16
  • 34