0

I am using the FragmentPagerAdapter in my application. In the Adapter I have the following code:

@Override
    public Fragment getItem(final int position) {
        switch(position){
        case 0:
            return OffersBasicInfoFragment.newInstance();
        case 1:
            return OffersSelectedCategoriesFragment.newInstance();
        case 2:
            return OffersManageArtworkFragment.newInstance();
        case 3:
            return OffersLegalDisclaimerFragment.newInstance();
        case 4:
            return OffersSelectStoresFragment.newInstance();
        case 5:
            return OffersPreviewSaveOfferFragment.newInstance();
        default:
            return null;
        }
    }

Now in my onViewCreated function I have various network calls. Now all of these seven pages get called at the same time. How do I structure my code such that only when the current page is active are the calls made. Meaning when I swipe from Tab1 to Tab2 should the calls in the Tab2 fragment be called.

How can I do this ?

Trinimon
  • 13,839
  • 9
  • 44
  • 60
user1730789
  • 5,157
  • 8
  • 36
  • 57

1 Answers1

0

You probably suffer from poor Fragment design.

Strange is that ViewPager should pre-cache only like first 2-3 fragments (including the one currently visible)

Maybe using setOffscreenPageLimit (int limit) could give you possibility to create only current fragment, or not more than two at the same time.

http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit(int)


Steps I'd have checked if I were you

  1. Use Service and/or ContentProvider to avoid caching and concurrency problems
  2. Try to use Fragment.setUserVisibleHint to start network call when user can really see the Fragment contents
  3. If using HttpClient, try to create singleton and set maxConnectionsPerHost and maxTotalConnections to allow one request at time

Links to study on:

Community
  • 1
  • 1
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244