title: FragmentStatePager Adapter IndexOutOfBound on Background
body: <p>I'm implementing a dynamic viewpager using an adapter that extends FragmentStatePagerAdapter. </p>
  
  <p>Every time my app goes to the background it crashes. with IndexOutOfBounds on ViewPager:</p>
  
  <p>Here's Adapter Code:</p>
  
  <pre><code>/**
   * Created by root on 3/11/15.
   */
  public class FragmentAdapter extends FragmentStatePagerAdapter {
      private ArrayList&lt;Fragment&gt; mFragList = null;
      private ArrayList&lt;String&gt; mFragmentNameList = null;
  
      public FragmentAdapter(FragmentManager fm, ArrayList&lt;Fragment&gt; mFragList, ArrayList&lt;String&gt; fragmentNameList) {
          super(fm);
          this.mFragList = mFragList;
          this.mFragmentNameList = fragmentNameList;
      }
  
      @Override
      public Fragment getItem(int position) {
          if (!AppUtility.isNullOrEmpty(mFragList) &amp;&amp; position &lt; mFragList.size()) {
              return mFragList.get(position);
          }
          return null;
      }
  
      @Override
      public int getCount() {
          if (mFragList != null) {
              return mFragList.size();
          }
          return 0;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          return mFragmentNameList.get(position);
      }
  }
  </code></pre>
  
  <p>Here's Fragment Code:</p>
  
  <pre><code> public class PagerFragment extends BaseFragment {
  
          private final int MAX_PAGES = 4;
          private ViewPager mViewPager = null;
          private FragmentAdapter mFragmentAdapter = null;
          private ArrayList&lt;Fragment&gt; mFragList = new ArrayList&lt;&gt;(MAX_PAGES);
          private ArrayList&lt;String&gt; mFragmentNameList = new ArrayList&lt;&gt;(MAX_PAGES);
          private TabLayout mTabLayout = null;
  
  
  
      @Override
      public void onViewCreated(View view, Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
          initViewById(view);
      }
  
      public void initViewById(View view) {
          mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
          mTabLayout = (TabLayout) view.findViewById(R.id.tabs);
  
          mFragList.add(new Fragment());
          mFragList.add(new Fragment());
          mFragList.add(new Fragment());
          mFragList.add(new Fragment());
          mFragmentNameList.add("Fragment1");
          mFragmentNameList.add("Fragment2");
          mFragmentNameList.add("Fragment3");
          mFragmentNameList.add("Fragment4");
          mFragmentAdapter = new FragmentAdapter(getFragmentManager(), mFragList, mFragmentNameList);
          mViewPager.setOffscreenPageLimit(2);
          mViewPager.setAdapter(mFragmentAdapter);
          mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
          mTabLayout.setupWithViewPager(mViewPager);
      }
  }
  </code></pre>
  
  <p>It does not shows any line number for my classes but only for viewpager:</p>
  
  <p>Error Log:</p>
  
  <pre><code>FATAL EXCEPTION: main
  Process: com.android.myapp.ui, PID: 5897
  java.lang.IndexOutOfBoundsException: Invalid index 5, size is 3
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
    at java.util.ArrayList.get(ArrayList.java:308)
    at android.support.v4.view.ViewPager.getChildDrawingOrder(ViewPager.java:800)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3396)
    at android.view.View.draw(View.java:16384)
    at android.support.v4.view.ViewPager.draw(ViewPager.java:2415)
    at android.view.View.updateDisplayListIfDirty(View.java:15378)
    at android.view.View.draw(View.java:16151)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
    at android.view.View.updateDisplayListIfDirty(View.java:15373)
    at android.view.View.draw(View.java:16151)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
    at android.view.View.draw(View.java:16384)
    at android.view.View.updateDisplayListIfDirty(View.java:15378)
    at android.view.View.draw(View.java:16151)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
    at android.view.View.draw(View.java:16384)
    at android.view.View.updateDisplayListIfDirty(View.java:15378)
    at android.view.View.draw(View.java:16151)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
    at android.view.View.updateDisplayListIfDirty(View.java:15373)
    at android.view.View.draw(View.java:16151)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
    at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1383)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
    at android.view.View.draw(View.java:16384)
    at android.view.View.updateDisplayListIfDirty(View.java:15378)
    at android.view.View.draw(View.java:16151)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
    at android.view.View.updateDisplayListIfDirty(View.java:15373)
    at android.view.View.draw(View.java:16151)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
    at android.view.View.updateDisplayListIfDirty(View.java:15373)
    at android.view.View.draw(View.java:16151)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
    at android.view.View.updateDisplayListIfDirty(View.java:15373)
    at android.view.View.draw(View.java:16151)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
    at android.view.View.updateDisplayListIfDirty(View.java:15373)
    at android.view.View.draw(View.java:16151)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3610)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3400)
    at android.view.View.draw(View.java:16384)
    at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2692)
    at android.view.View.updateDisplayListIfDirty(View.java:15378)
    at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
    at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
    at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2928)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2740)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2358)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1233)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6549)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:919)
    at android.view.Choreographer.doCallbacks(Choreographer.java:710)
    at android.view.Choreographer.doFrame(Choreographer.java:645)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:905)
      at android.os.Han
  </code></pre>