title: ViewPager wrong page
body: <p>I'm making an app with horizontal pages using ViewPager class. Currently, having problems with displaying pages correctly. This code mostly comes from example, with few modifications. I'm not sure how the fragment "switch" should happen, but I got it semi-working at onCreateView. Now when I start app, I get welcome screen, next page is again welcome screen, then page1/2/3. When "moving backwards" it goes like page 3, 2, 3, 2, 1.
  Probably there's problem with mViewPager.getCurrentItem(), but I'm clueless why.</p>
  
  <pre><code>    public class MainActivity extends ActionBarActivity {
  
          SectionsPagerAdapter mSectionsPagerAdapter;
          static ViewPager mViewPager;
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
  
              getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
  
              final ActionBar actionBar = getSupportActionBar();
              actionBar.hide(); 
              setContentView(R.layout.activity_main);
              actionBar.setCustomView(R.layout.custom_actionbar);
              actionBar.setDisplayShowTitleEnabled(false);
              actionBar.setDisplayShowCustomEnabled(true); 
              actionBar.setDisplayUseLogoEnabled(false);
              actionBar.setDisplayShowHomeEnabled(false);
  
              mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
  
              mViewPager = (ViewPager) findViewById(R.id.pager);
              mViewPager.setAdapter(mSectionsPagerAdapter);
  
          }
  
          public class SectionsPagerAdapter extends FragmentPagerAdapter {
  
              public SectionsPagerAdapter(FragmentManager fm) {
                  super(fm);
              }
  
              @Override
              public Fragment getItem(int position) {
                  return PlaceholderFragment.newInstance(position + 1);
              }
  
              @Override
              public int getCount() {
                  return 5;
              }    
          }
          public static class PlaceholderFragment extends Fragment {
  
              private static final String ARG_SECTION_NUMBER = "section_number";
  
              public static PlaceholderFragment newInstance(int sectionNumber) {
                  PlaceholderFragment fragment = new PlaceholderFragment();
                  Bundle args = new Bundle();
                  args.putInt(ARG_SECTION_NUMBER, sectionNumber);
                  fragment.setArguments(args);
                  return fragment;
              }
  
              public PlaceholderFragment() {
              }
  
              @Override
              public View onCreateView(LayoutInflater inflater, ViewGroup container,
                      Bundle savedInstanceState) {
                  int page = mViewPager.getCurrentItem();
                  View rootView;
                  switch (page) {
                      case 0: rootView = inflater.inflate(R.layout.welcome_screen, container, false);
                          break;
                      case 1: rootView = inflater.inflate(R.layout.page1, container, false);
                          break;
                      case 2: rootView = inflater.inflate(R.layout.page2, container, false);
                          break;
                      case 3: rootView = inflater.inflate(R.layout.page3, container, false);
                          break;
                      case 4: rootView = inflater.inflate(R.layout.page4, container, false);
                          break;
                      case 5: rootView = inflater.inflate(R.layout.page5, container, false);
                          break;
                      default: rootView = inflater.inflate(R.layout.welcome_screen, container, false);
                  }
              }
          }
      }
  </code></pre>