title: Android ViewPager Loading fragments in wrong sequence
body: <p>I have a spinner having values from week 1 to 12. On selection of any week from spinner, 7 fragments(starting from sunday to monday) are loaded in my viewpager and tablayout. The only difference is in data for each week and day. </p>
  
  <p>Now the issue is for each week correct data is populated in sunday fragment. When I slide to monday fragment the data populated there is of tuesday and so on. </p>
  
  <p>Below is my viewpagerAdapter class</p>
  
  <pre><code>viewPager = (ViewPager) mView.findViewById(R.id.viewpager);
      setupViewPager(viewPager, 1);
      TabLayout mTabLayout = (TabLayout)  mView.findViewById(R.id.tabs);
      mTabLayout.setupWithViewPager(viewPager);
  
  @Override
  public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
          setupViewPager(viewPager, position + 1);
  }
  
  private void setupViewPager(ViewPager viewPager, int weekNo) {
      ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
  
      final SunFragment sunFragment = new SunFragment();
      Bundle sunBundle = new Bundle();
      sunBundle.putInt(DayFragment.KEY_WEEK_NO, weekNo);
      sunFragment.setArguments(sunBundle);
      adapter.addFragment(sunFragment, "Sunday");
  
      final MonFragment monFragment = new MonFragment();
      Bundle monBundle = new Bundle();
      monBundle.putInt(DayFragment.KEY_WEEK_NO, weekNo);
      monFragment.setArguments(monBundle);
      adapter.addFragment(monFragment, "Monday");
  
      final TuesFragment tuesFragment = new TuesFragment();
      Bundle tuesBundle = new Bundle();
      tuesBundle.putInt(DayFragment.KEY_WEEK_NO, weekNo);
      tuesFragment.setArguments(tuesBundle);
      adapter.addFragment(tuesFragment, "Tuesday");
  
      final WedFragment wedFragment = new WedFragment();
      Bundle wedBundle = new Bundle();
      wedBundle.putInt(DayFragment.KEY_WEEK_NO, weekNo);
      wedFragment.setArguments(wedBundle);
      adapter.addFragment(wedFragment, "Wednesday");
  
      final ThursFragment thursFragment = new ThursFragment();
      Bundle thursBundle = new Bundle();
      thursBundle.putInt(DayFragment.KEY_WEEK_NO, weekNo);
      thursFragment.setArguments(thursBundle);
      adapter.addFragment(thursFragment, "Thursday");
  
      final FriFragment friFragment = new FriFragment();
      Bundle friBundle = new Bundle();
      friBundle.putInt(DayFragment.KEY_WEEK_NO, weekNo);
      friFragment.setArguments(friBundle);
      adapter.addFragment(friFragment, "Friday");
  
      final SatFragment satFragment = new SatFragment();
      Bundle satBundle = new Bundle();
      satBundle.putInt(DayFragment.KEY_WEEK_NO, weekNo);
      satFragment.setArguments(satBundle);
      adapter.addFragment(satFragment, "Saturday");
  
      viewPager.setAdapter(adapter);
  }
  
  static class ViewPagerAdapter extends FragmentStatePagerAdapter {
      private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
      private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
      ViewPagerAdapter(FragmentManager manager) {
          super(manager);
      }
  
      @Override
      public Fragment getItem(int position) {
          return mFragmentList.get(position);
      }
  
      @Override
      public int getCount() {
          return mFragmentList.size();
      }
  
      void addFragment(Fragment fragment, String title) {
          mFragmentList.add(fragment);
          mFragmentTitleList.add(title);
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return mFragmentTitleList.get(position);
      }
  }
  </code></pre>
  
  <p>Fragments are loaded in below sequence:</p>
  
  <blockquote>
    <p>when sliding Right</p>
    
    <p>fragment -> data loaded</p>
    
    <p>sun        sun</p>
    
    <p>mon        tues</p>
    
    <p>tues        wed</p>
    
    <p>wed        thurs</p>
    
    <p>thurs        fri</p>
    
    <p>fri        sat</p>
    
    <p>sat        sat</p>
    
    <p>when sliding left</p>
    
    <p>fragment -> data loaded</p>
    
    <p>sat        sat</p>
    
    <p>fri        thurs</p>
    
    <p>thurs        wed</p>
    
    <p>wed        tues</p>
    
    <p>tues        mon</p>
    
    <p>mon        sun</p>
    
    <p>sun        sun                      </p>
  </blockquote>