title: Replace current Fragment in ViewPager on Android
body: <p>I have a <code>ViewPager</code>  and I have to replace the first <code>Fragment</code> if a certain action happens. </p>
  
  <pre><code>public static class PagerAdapter extends FragmentStatePagerAdapter{
  
      private TempChannel latestChannel;
      private VideosFragment.SortType sortType;
  
      public PagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
          switch (position){
              case 0:
                  if(latestChannel == null) {
                      return new LatestVideosFragment();
                  }else{
                      return VideosFragment.getVideosFragment(latestChannel, sortType);
                  }
              case 1:
                  return new LiveVideosFragment();
              case 2:
                  return new ConversationsFragment();
          }
          return null;
      }
  }
  </code></pre>
  
  <p>Basically, when I click a channel in the <code>LatestVideosFragment</code>, the first position in the adapter should change to an instance of the <code>VideosFragment</code> class.</p>
  
  <pre><code>public void startLatestChannelFragment(TempChannel channel, VideosFragment.SortType sortType){
      adapter.setLatestChannel(channel, sortType);
      adapter.notifyDataSetChanged();
      Log.d("x", "on channel started");
  }
  </code></pre>
  
  <p>I assumed that calling <code>notifyDataSetChanged()</code> on the adapter would update the fragment, but it does not. Also tried calling <code>invalidate(</code>) on the <code>ViewPager</code> without any effect. The <code>Fragment</code> is updated only if I go to the third tab, and then return to the first.</p>