How to get the selected page index of ViewPager in fragment

I’m making use of ViewPagerIndicator, and I have my adapter defined like this:

public class MyPagerAdapter extends FragmentPagerAdapter implements
    IconPagerAdapter {

private final static String TAB_ITEMS[] = { "Tab 1", "Tab 2", "Tab 3", "Tab 4"};

private int count = TAB_ITEMS.length;

public MyPagerAdapter (FragmentManager fm) {
    super(fm);
}

@Override
public int getIconResId(int index) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = new BaseFragment();

    return fragment;
}

@Override
public int getCount() {
    return count;
}

And this is my activity that will contain my fragments:

public class MyActivity extends FragmentActivity implements
    OnPageChangeListener {

private int tabPosition;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    ViewPager pager = (ViewPager) findViewById(R.id.pager);
    MyPagerAdapter adapter = new MyPagerAdapter (
            getSupportFragmentManager());
    pager.setAdapter(adapter);

    UnderlinePageIndicator indicator = (UnderlinePageIndicator) findViewById(R.id.indicator);
    indicator.setFades(false);
    indicator.setViewPager(pager);
    indicator.setOnPageChangeListener(this);
}

@Override
public void onPageScrollStateChanged(int state) {
    // TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int position, float positionOffset,
        int positionOffsetPixels) {
    // TODO Auto-generated method stub

}

@Override
public void onPageSelected(int position) {
    // TODO Auto-generated method stub
    setTabPosition(position);

    Toast.makeText(MyActivity.this,
            "Changed to page: " + position, Toast.LENGTH_SHORT).show();
}

public void setTabPosition(int position) {
    this.tabPosition = position;
}

public int getTabPosition() {
    return this.tabPosition;
}

}

Finally the definition of BaseFragment:

public class BaseFragment extends Fragment implements OnItemClickListener {
private final static String TAG = "BaseFragment";

private int tabPosition;

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

    View view = inflater.inflate(R.layout.fragment_base, container,
            false);

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    super.onViewCreated(view, savedInstanceState);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    tabPosition = ((MyActivity) getActivity()).getTabPosition();

    Toast.makeText(getActivity(), "page " + tabPosition, Toast.LENGTH_SHORT)
            .show();

}

Now , this is my problem:

The toast in onPageSelected of the activity always shows the correct page when a new page is selected or swiped to.

However, the toast in my fragment only shows the correct page when you swipe to either page 1 or page 2. The only time it toasts page 0 is anytime the app is launched. After that, when the user swipes to page 0 or page 4, no toast is shown at all, not even an empty toast.

I wrote the getTabPosition() and setTabPosition() methods with the intention of getting the current selected tab position in the fragment, but it obviously doesn’t work correctly hence I dont get toasts on pages 0 and 4.

My question is: how do I get the selected page position in my BaseFragment, since I need that position to load the appropriate data?

Hope I was clear enough?

Answer

Each Fragment will display a different set of data, then maybe it is a good idea to have one fragment for each. Here is what I did to display different Fragments for different data in the FragmentPagerAdapter:

@Override
    public Fragment getItem(int index) {

        Fragment fragment = null;

        // Define the page for each index
        switch(index){

            case 0:
                fragment = new Fragment1();
                break;
            case 1:
                fragment = new Fragment2();
                break;
            case 2:
                fragment = new Fragment3();
                break;

        }

This may not be the best approach but in your activity that holds reference to ViewPager try this:

public class TabbedViewActivity extends FragmentActivity {

    ViewPager viewPager;
    public static int tabIndex = 0;
    @Override
    protected void onCreate(Bundle bundle) {
        ......
        viewPager = (ViewPager)findViewById(R.id.pager);
        TabbedViewActivity.tabIndex = 0; // in the start index is always 0 unless coded otherwise
        viewPager.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                         TabbedViewActivity.tabIndex = position;

                    }
                });

}

Then you can always access this variable statically like this in your Fragment TabbedViewActivity.tabIndex