0

So I created an Android application project using ActionBar Tabbed Activity using ViewPager. I've also created two fragment classes and attached corresponding fragment layout with them. As the respective tab is selected I return fragment to SectionsPagerAdapter.

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        switch (position) {
        case 0:
            return new fragmentAddSize();
        case 1:
            return new fragmentMySizes();
    }
    return null;
    }
} 

Now I have some questions about this. I was under the impression that every time a tab is selected, it calls the fragment class and goes into the onCreateView of the fragment but it only happens once.

What I want to achieve is to know when the tabs are selected and the corresponding fragment is in view. So that everytime i switch tabs it just pops a tab sying you are in tab 1 or tab2.

iitum studant
  • 856
  • 2
  • 8
  • 24

1 Answers1

0

Add a pageChangeListener to your pager.

http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html

reactivemobile
  • 505
  • 3
  • 9
  • I don't want to add anything in the MainActivity. Is it possible to have something in the fragment class? – iitum studant Oct 30 '14 at 16:34
  • The accepted answer on this question should do the trick http://stackoverflow.com/questions/9779397/detect-viewpager-tab-change-inside-fragment – reactivemobile Oct 30 '14 at 17:12