title: ViewPager update fragment on swipe
body: <p>i have a problem that i have been struggling with for the past 2 days. </p>
  
  <p>I am building an app that uses ActionBar, ViewPager &amp; FragmentPagerAdapter.
  The code for the Activity, Fragments &amp; FragmentPagerAdapter are exactly as the ones stated in the android example on <a href="http://developer.android.com/reference/android/support/v4/view/ViewPager.html">http://developer.android.com/reference/android/support/v4/view/ViewPager.html</a></p>
  
  <p>The problem i am facing is -- assuming i have only 2 fragments in the viewPager. when switching/swiping between the two, the fragments are not getting updated. onResume does not get called because the viewPager caches a minimum of 1 fragment to either side of the displayed fragment.</p>
  
  <p>I tried using the onTabSelected to detect when a fragment is selected and then start a method from that fragment with the help of an interface (code below).</p>
  
  <pre><code>public void onTabSelected(Tab tab, FragmentTransaction ft) {
      TabInfo tag = (TabInfo)tab.getTag();
      for (int i=0; i&lt;mTabs.size(); i++) {
          if (mTabs.get(i) == tag) {
              mViewPager.setCurrentItem(i);
          }
      }
      ((IStartStop)getItem(tab.getPosition())).Start();
  }
  </code></pre>
  
  <p>However, when the Start method is used a NullPointerException is fired when trying to update a textview. The start method's code is:</p>
  
  <pre><code>public void Start() {
      TextView tv = _view.findViewById(R.id.text);
      tv.setText("test");
  }
  </code></pre>
  
  <p>The exception is thrown at line:</p>
  
  <pre><code>TextView tv = _view.findViewById(R.id.text);
  </code></pre>
  
  <p>The IStartStop interface is quite simple:</p>
  
  <pre><code>public interface IStartStop {
      public void Start();
      public void Stop();
  }
  </code></pre>
  
  <p>I don't want to use notifyDataSetChanged(); with POSITION_NONE because every time I swipe to a new fragment, it takes a few seconds to load the fragments</p>
  
  <p>At this time, the fragments only include a textview, in the future they will have an animation and so it is important to:</p>
  
  <p>1- Only run an animation when the fragment is selected and not when the fragment next to it is selected (the way ViewPager caches and resumes fragments).</p>
  
  <p>2- Stop the animation when the fragment is no longer selected to avoid wasting device resources.</p>
  
  <p>Yes, i already checked everything available on the internet but nothing seems to work with me.</p>
  
  <p>Thank you very much for your help!</p>