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.
TabsPagerAdaptercompletely!! - mmlooloo