title: Viewpager showing correct fragment but not initializing it
body: <p>I have looked everywhere but in every post the problem seems to persist. I have implemented a viewpager using FragmentStatePageAdapter. While the fragments are being displayed correctly on the UI, when I try to pass a value to the fragment the app crashes since the fragment isn't initialized. To check I put a log in each section and only the first and the third log showed up. Here's my code. </p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>public class MainActivity extends FragmentActivity implements      OnDataReceivedListener, OnStateChangedListener {
  
      ViewPager viewPager;
      PagerAdapter pagerAdapter;
      private ImageView logo;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          logo = (ImageView)findViewById(R.id.logo);
  
          viewPager = (ViewPager)findViewById(R.id.viewpager_first);
          pagerAdapter= new PagerAdapter(getSupportFragmentManager(),3);
          CircleIndicator indicator (CircleIndicator)findViewById(R.id.indicator);
          viewPager.setAdapter(pagerAdapter);
          indicator.setEnabled(true);
          indicator.setViewPager(viewPager);
  
          logo.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  flipApi.startScan();
              }
          });
  
      }
  
  
      class PagerAdapter extends FragmentStatePagerAdapter {
  
          int num;
          public PagerAdapter(FragmentManager fm, int x) {
              super(fm);
              this.num=x;
          }
  
          public Fragment currentFragment;
  
          @Override
          public Fragment getItem(int position) {
              //add new fragment here for homepage viewpager
  
              switch (position) {
  
                  case 0:
                      currentFragment = new Calorie_Fragment();
                      Log.d("flip",1+"");
                      return currentFragment;
  
  
                  case 1:
                      currentFragment = new HeartRate_Fragment();
                      Log.d("flip",2+"");
                      return currentFragment;
  
  
                  case 2:
                      currentFragment = new Steps_Fragment();
                      Log.d("flip",3+"");
                      return currentFragment;
  
  
                  default:
                      return null;
  
              }
  
          }
  
          @Override
          public int getCount() {
              return num;
          }
  
          public Fragment getCurrentFragment() {
              return  this.currentFragment;
          }
      }
  }
  </code></pre>
  
  <p>Here's my log:</p>
  
  <pre><code>04-19 17:43:40.374 30949-30949/com.example.riche.flip_final D/flip: 1
  04-19 17:43:40.388 30949-30949/com.example.riche.flip_final D/flip: 2
  04-19 17:43:41.386 30949-30949/com.example.riche.flip_final 
  I/Choreographer:Skipped 61 frames!  The application may be doing too 
  much work on its main thread.
  04-19 17:43:44.539 30949-30949/com.example.riche.flip_final D/flip: 3
  04-19 17:43:44.560 30949-30949/com.example.riche.flip_
  final W/FragmentManager: moveToState: Fragment state for Steps_Fragment{33bf8e3 #2 id=0x7f0d0075} not updated inline; expected state 3 found 2
  04-19 17:43:46.999 30949-30949/com.example.riche.flip_final D/flip: 1
  04-19 17:43:47.008 30949-30949/com.example.riche.flip_final  W/FragmentManager: moveToState: Fragment state for Calorie_Fragment{208d45e #1  id=0x7f0d0075} not updated inline; expected state 3 found 2
  04-19 17:43:48.105 30949-30949/com.example.riche.flip_final D/flip: 3
  04-19 17:43:48.115 30949-30949/com.example.riche.flip_final W/FragmentManager: moveToState: Fragment state for Steps_Fragment{fd45d27 #2  id=0x7f0d0075} not updated inline; expected state 3 found 2
  </code></pre>