title: FragmentPagerAdapter getItem() is returning more than it should
body: <p>I have a <code>MainActivity</code> that controls four fragments, each of which is a tab. When my main activity starts, I have a line being printed to the log to show me what fragment is being instantiated. Here is my FragmentPagerAdapter:</p>
  
  <pre><code>public class TabsPagerAdapter extends FragmentPagerAdapter {
  
  public TabsPagerAdapter(FragmentManager fm) {
      super(fm);
  }
  
  @Override
  public Fragment getItem(int index) {
  
      switch (index) {
          case 0:
              System.out.println("Returning new PrearrivalPlan()");
              return new PrearrivalPlan();
          case 1:
              System.out.println("Returning new PrimarySurvey()");
              return new PrimarySurvey();
          case 2:
              System.out.println("Returning new SecondarySurvey()");
              return new SecondarySurvey();
          case 3:
              System.out.println("Returning new PrepareForTravel()");
              return new PrepareForTravel();
      }
  
      return null;
  }
  
  @Override
  public int getCount() {
      // get item count - equal to number of tabs
      return 4;
  }
  
  }
  </code></pre>
  
  <p>The tab bar has the following options in order:</p>
  
  <p><code>Prearrival Plan | Primary Survey | Secondary Survey | Prepare for Travel</code></p>
  
  <p>When my main activity starts, the following is printed to the screen: </p>
  
  <p><code>Returning new PrearrivalPlan()
   Returning new PrimarySurvey()</code></p>
  
  <p>What it seems to be doing is loading one tab ahead of the one I have selected. Since PrearrivalPlan is the first tab, I would think it should just return a new <code>PrearrivalPlan()</code> except it returns both. Another example, when I click on Primary Survey tab (second tab), the following is printed to the screen: </p>
  
  <p><code>Returning new SecondarySurvey() // &lt;- This is the third tab!?</code></p>
  
  <p>Because PrimarySurvey was already instantiated when the activity first started (see output above), it jumped ahead just like it did before and loaded the third tab even though I only clicked on the second.</p>
  
  <p>Here is my <code>MainActivity</code>:</p>
  
  <pre><code>public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
  
  private CustomViewPager viewPager;
  private TabsPagerAdapter mAdapter;
  private ActionBar actionBar;
  
  private String[] tabTitles = {"Pre-arrival Plan", "Primary Survey", "Secondary Survey", "Prepare for Travel"};
  
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      viewPager = (CustomViewPager) findViewById(R.id.pager);
      actionBar = getActionBar();
      mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
  
      viewPager.setAdapter(mAdapter);
      viewPager.setPagingEnabled(false);
  
      actionBar.setHomeButtonEnabled(true);
      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
  
      viewPager.setOnPageChangeListener(new CustomViewPager.OnPageChangeListener() {
  
  
          @Override
          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  
          }
  
          @Override
          public void onPageSelected(int position) {
              actionBar.setSelectedNavigationItem(position);
          }
  
          @Override
          public void onPageScrollStateChanged(int state) {
  
          }
  
      });
  
      // Remove Android icon from Action Bar
      getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
      getActionBar().setDisplayShowHomeEnabled(false);
  
      // Add tabs to Action Bar
      for (String tab_name : tabTitles) {
          actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
      }
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
  }
  
  public void updateTabTitles(int tabNumber, int checkBoxesRemaining) {
      String text = tabTitles[tabNumber] + " \n             (" + checkBoxesRemaining + ")";
      actionBar.getTabAt(tabNumber).setText(text);
  }
  
  @Override
  public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
      viewPager.setCurrentItem(tab.getPosition());
  }
  
  @Override
  public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
  
  }
  
  @Override
  public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
  
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case android.R.id.home:
              Intent intent = new Intent(this, MainMenu.class);
              startActivity(intent);
              return true;
          case R.id.complete:
              DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      switch (which){
                          case DialogInterface.BUTTON_POSITIVE:
                              goToReport();
                              break;
                          case DialogInterface.BUTTON_NEGATIVE:
                              //No button clicked
                              break;
                      }
                  }
              };
              AlertDialog.Builder builder = new AlertDialog.Builder(this);
              builder.setMessage("Are you sure you want to complete the checklist?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
              return true;
          default:
              return super.onOptionsItemSelected(item);
      }
  }
  
  public void goToReport() {
      Intent intent = new Intent(this, Report.class);
      startActivity(intent);
  }
  </code></pre>