title: How to load fragment data only when its tab is clicked in PagerSlidingTabStrip
body: <p>I am using <code>PagerSlidingTabStrip</code> in my project and am showing data in fragments. Its a great library which works great. Each Fragment consists of a scholar's lectures which are loaded from a web service. The code to access the web service is in <code>onCreateView</code> method of the Fragment. Currently all the fragment's data is loaded at the same time when they are added to <code>ViewPager</code>. I was wondering if there is any way of loading the Fragment's data only when a user clicks/swipes to a specific tab. Not everything at a time.</p>
  
  <p>Current code is pretty standard, I took help from the provided example [android solution].<a href="https://github.com/astuetz/PagerSlidingTabStrip" rel="noreferrer">1</a></p>
  
  <p>Pager Adapter is as follows:</p>
  
  <pre><code>public class ScholarDetailPagerAdapter extends FragmentPagerAdapter {
  
      private final String[] TITLES = { "Scholar Info", "Lectures"};
      Scholar _scholar;
  
      public ScholarDetailPagerAdapter(FragmentManager fm, Scholar scholar) {
          super(fm);
          _scholar = scholar;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return TITLES[position];
      }
  
      @Override
      public Fragment getItem(int arg0) {
          if(arg0 == 0)
              return ScholarsInfoFragment.netInstance(_scholar.Name, _scholar.Information, _scholar.ThumbnailUrl);
          else
              return ScholarLecturesFragment.newInstance(_scholar.ScholarId);
      }
  
      @Override
      public int getCount() {
          return TITLES.length;
      }
  
  }
  </code></pre>
  
  <p>It returns TWO fragments, Info and Lectures. Lectures fragment calls a web service to get a scholar's lectures.</p>
  
  <p>Activity code is:</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      // set the Above View
      setContentView(R.layout.tabbed_view);
      Scholar scholar = getScholar(getIntent());      
  
      _tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
      _pager = (ViewPager) findViewById(R.id.pager);
      _adapter = new ScholarDetailPagerAdapter(getSupportFragmentManager(), scholar);
  
      _pager.setAdapter(_adapter);
  
      final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
              .getDisplayMetrics());
      _pager.setPageMargin(pageMargin);
  
      _tabs.setViewPager(_pager);
  } 
  </code></pre>
  
  <p>I tried adding <code>_tabs.setOnPageChangeListener(this);</code> and ended up having <code>onPageSelected(int arg0)</code> in my activity as well. I thought It would be easy now. Then I modified <code>public Fragment getItem(int arg0)</code> in <code>ScholarDetailPagerAdapter</code> above and returned <code>null</code> instead of returning two fragments. But that raised an exception. I thought It would render empty views in pager .. but of course I was wrong.</p>
  
  <p>To make sure Fragment loads data when its respective tab is clicked and is visible, I also have tried overriding its following events and wrote the data loading call in them:</p>
  
  <ol>
  <li><p><code>onResume()</code> Not worked. Still loads data when added in activity</p></li>
  <li><p>Tried overriding onHiddenChanged </p>
  
  <pre><code>@Override   
  public void onHiddenChanged(boolean hidden) {
  
      super.onHiddenChanged(hidden);
  
      if(!hidden){
          loadLectures();
      }
  </code></pre>
  
  <p>}</p></li>
  </ol>
  
  <p>This did not work too. No data loaded as if this event is not fired at all.</p>
  
  <p>How do I do that? I simply want to load a fragment when user clicks on a tab.</p>
  
  <p>Thanks in advance</p>