title: Android ViewPager Loading incorrect fragments
body: <p>So I'm trying to get a tab bar and layout underneath my main navigation bar working on my main activity.</p>
  
  <p>I can't seem to get the ViewPager/TabLayout (unsure which is the cause of the problem, new to this) to load the correct fragment. I've read through a ton of documentation and checked more than 1 tutorial to see if I was doing this correctly and it seems that I'm doing the same thing they are...</p>
  
  <p>The <code>OnTabSelectedListener</code> for my <code>TabLayout</code> is seeing the tab changes properly, and in <code>OnTabSelected</code> Im calling <code>viewPager.setCurrentItem(tab.getPosition())</code> to attempt to set my viewpagers item, but it's still not working. Upon first load, it loads the first and second tabs (I can see via a sent API request from the fragments onCreate methods), and when I click on the other tabs, it consistently loads the wrong fragment, sometimes not even loading one at all!</p>
  
  <p>Not sure if this is related but I'm getting this warning as I switch tabs:</p>
  
  <p><code>03-03 16:24:22.940 16967-16967/com.sampleapp.app W/FragmentManager: moveToState: Fragment state for BlankFragment3{efddbec #2 id=0x7f0c007b android:switcher:2131492987:2} not updated inline; expected state 3 found 2</code></p>
  
  <p>I'm using build tools version <code>23.0.2</code> and the following dependencies:</p>
  
  <pre><code>compile 'com.android.support:appcompat-v7:23.1.1'
  compile 'com.android.support:design:23.1.1'
  compile 'com.android.support:support-v4:23.1.1'
  </code></pre>
  
  <p>This is my layout code for my main nav bar:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;android.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/AppTheme.AppBarOverlay"&gt;
  
      &lt;android.support.v7.widget.Toolbar
          android:id="@+id/toolbar1"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:background="?attr/colorPrimary"
          app:popupTheme="@style/AppTheme.PopupOverlay" /&gt;
  
      &lt;android.support.v7.widget.Toolbar
          android:id="@+id/toolbar2"
          android:layout_width="match_parent"
          android:layout_height="10dp"
          android:background="?attr/colorPrimary"
          app:layout_scrollFlags="scroll|enterAlways"
          app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /&gt;
  
      &lt;android.support.design.widget.TabLayout
          android:id="@+id/tab_layout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:tabMode="fixed"
          app:tabGravity="fill"/&gt;
  
  &lt;/android.support.design.widget.AppBarLayout&gt;
  
  &lt;android.support.v4.view.ViewPager
      android:id="@+id/viewpager"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /&gt;
  
  &lt;android.support.design.widget.FloatingActionButton
      android:id="@+id/fab"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom|end"
      android:layout_margin="@dimen/fab_margin"
      android:src="@android:drawable/ic_input_add" /&gt;
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p>As per more than one tutorial, here is the code I'm using for my adapter</p>
  
  <pre><code>public class TabsAdapter extends FragmentPagerAdapter{
      private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
      private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
      public TabsAdapter(FragmentManager fm) {
          super(fm);
      }
  
      public void addFrag(Fragment fragment, String title){
          mFragmentList.add(fragment);
          mFragmentTitleList.add(title);
      }
  
      @Override
      public Fragment getItem(int position) {
          return mFragmentList.get(position);
      }
  
      @Override
      public int getCount() {
          return mFragmentList.size();
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return mFragmentTitleList.get(position);
      }
  }
  </code></pre>
  
  <p>And here is how I set up my viewpager and tablayout</p>
  
  <p>MainActivity's onCreate:</p>
  
  <pre><code>    ViewPager = (ViewPager) findViewById(R.id.viewpager);
      setupViewPager(mViewPager); // Method defined below
  
      TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
      tabLayout.setupWithViewPager(mViewPager);
      tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
          @Override
          public void onTabSelected(TabLayout.Tab tab) {
              mViewPager.setCurrentItem(tab.getPosition());
              Log.w("Tab Selection", String.valueOf(tab.getPosition()));
          }
  
          @Override
          public void onTabUnselected(TabLayout.Tab tab) {
  
          }
  
          @Override
          public void onTabReselected(TabLayout.Tab tab) {
  
          }
      });
  </code></pre>
  
  <p>And finally, my setupViewPager method:</p>
  
  <pre><code>private void setupViewPager(ViewPager viewPager) {
      TabsAdapter adapter = new TabsAdapter(getSupportFragmentManager());
      adapter.addFrag(new BlankFragment1(), "One");
      adapter.addFrag(new BlankFragment2(), "Two");
      adapter.addFrag(new BlankFragment3(), "Three");
      viewPager.setAdapter(adapter);
  }
  </code></pre>
  
  <p>If anyone sees anything wrong with this please let me know, I cannot for the life of me get these darn tabs to load the proper fragment.</p>
  
  <p>Thanks ahead of time!</p>