title: FragmentPagerAdapter - how to detect a swipe or a tab click when user goes to a new tab?
body: <p>I have a <code>MainActivity</code> that has three fragments in a <code>FragmentPagerAdapter</code> as below. How can I find out when an user goes from 1st fragment to second or from second to third, either with swiping or with a click on the tab? I saw that the <code>getItem()</code> method is not called always as I have declared <code>mViewPager.setOffscreenPageLimit(2)</code>;</p>
  
  <pre><code>public class MainThreeTabAdapter extends FragmentPagerAdapter {
  
      private final String[] CONTENT = new String[]{"News", "Rewards", "Me"};
  
      public MainThreeTabAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
          if (position == 0) {
              return NewsFragment.newInstance();
          } else if (position == 1) {
              return RewardsFragment.newInstance();
          } else if (position == 2) {
              return MeFragment.newInstance(true, App.getAccountData().getId());
          } else {
              return null;
          }
  
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return CONTENT[position % CONTENT.length];
      }
  
      @Override
      public int getCount() {
          return CONTENT.length;
      }
  }
  </code></pre>
  
  <h3>In the MainActivity's <code>onCreate()</code></h3>
  
  <pre><code>    mainThreeTabAdapter = new MainThreeTabAdapter(getFragmentManager());
  
      // Set up the ViewPager with the sections adapter.
      // this ensures that 2 tabs on each side of current are kept in memory, which is all we need for our case. Default = 1
      // this is all taken from the Quickreturn facebook sample app
      mViewPager.setOffscreenPageLimit(2);
      mViewPager.setAdapter(mainThreeTabAdapter);
  </code></pre>