Aamir Aamir - 1 month ago 4x
Android Question

How to load fragment data only when its tab is clicked in PagerSlidingTabStrip

I am using

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
method of the Fragment. Currently all the fragment's data is loaded at the same time when they are added to
. 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.

Current code is pretty standard, I took help from the provided example [android solution].1

Pager Adapter is as follows:

public class ScholarDetailPagerAdapter extends FragmentPagerAdapter {

private final String[] TITLES = { "Scholar Info", "Lectures"};
Scholar _scholar;

public ScholarDetailPagerAdapter(FragmentManager fm, Scholar scholar) {
_scholar = scholar;

public CharSequence getPageTitle(int position) {
return TITLES[position];

public Fragment getItem(int arg0) {
if(arg0 == 0)
return ScholarsInfoFragment.netInstance(_scholar.Name, _scholar.Information, _scholar.ThumbnailUrl);
return ScholarLecturesFragment.newInstance(_scholar.ScholarId);

public int getCount() {
return TITLES.length;


It returns TWO fragments, Info and Lectures. Lectures fragment calls a web service to get a scholar's lectures.

Activity code is:

public void onCreate(Bundle savedInstanceState) {

// set the Above View
Scholar scholar = getScholar(getIntent());

_tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
_pager = (ViewPager) findViewById(R.id.pager);
_adapter = new ScholarDetailPagerAdapter(getSupportFragmentManager(), scholar);


final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()


I tried adding
and ended up having
onPageSelected(int arg0)
in my activity as well. I thought It would be easy now. Then I modified
public Fragment getItem(int arg0)
above and returned
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.

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:

  1. onResume()
    Not worked. Still loads data when added in activity

  2. Tried overriding onHiddenChanged

    public void onHiddenChanged(boolean hidden) {




This did not work too. No data loaded as if this event is not fired at all.

How do I do that? I simply want to load a fragment when user clicks on a tab.

Thanks in advance


You could override setUserVisibleHint event of the fragment to know if its visible to the user and then load your data. something like following:

boolean _areLecturesLoaded = false;

 public void setUserVisibleHint(boolean isVisibleToUser) {
     if (isVisibleToUser && !_areLecturesLoaded ) {
      _areLecturesLoaded = true;