0
votes

I have a TabsPagerAdapter class which extends FragmentPagerAdapter. I use it to populate 3 tabs. All the three views are Fragments.

This is how they are defined

public class DonorSearchFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ...
    }
}

public class DonorResultsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ...
    }
}

public class CampDonorResultsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ...
    }
}

These Fragments (as tabs) are instantiated from TabsPagerAdapter class like below

public class TabsPagerAdapter extends FragmentPagerAdapter {

    private ArrayList<Fragment> fragments;

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
        fragments = new ArrayList<Fragment>();
        fragments.add(Constants.SEARCH_TAB_POSITION, new DonorSearchFragment());
        fragments.add(Constants.DONORS_TAB_POSITION, new DonorResultsFragment());
        fragments.add(Constants.CAMP_DONORS_TAB_POSITION, new CampDonorResultsFragment());
    }
    ...
}

and in MainActivity, I instantiate the TabsPagerAdapter class which was shown above

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener, DonorSearchFragment.ResultsChangedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        tabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(tabsPagerAdapter);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }
        ..
    }
}

Surprisingly, the third tab(ie CampDonorResultsFragment) onCreateView() is not getting invoked even after instantiation as above. As a result, if I call any method on that Fragment, all the variables (instantiated in onCreateView() ) are null, and I get Null Pointer Exception.

What exactly happens here, why the onCreateView() is not called before any method on that Fragment is manually called. Also, why does not this happen on the Second Tab (DonorResultsFragment) above.

1
post TabsPagerAdapter completely!! - mmlooloo

1 Answers

2
votes

View pager only keeps the current page, plus a number of previous/next pages as specified by off-screen page limit which defaults to 1.

Consider redesigning your code so that it doesn't assume your fragments to be alive at all times. Instead of invoking methods on fragments, make the fragments pull out the data they need wherever it is available.

As a quick fix, you can set the page limit to a higher number like 2 but that won't fix the underlying design problems that will eventually bite you somewhere else.