title: Dynamically remove an item from a ViewPager with FragmentStatePagerAdapter
body: <p>There are quite a few discussions around this topic </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/a/7287121/494659">ViewPager PagerAdapter not updating the View</a> </li>
  <li><a href="https://stackoverflow.com/questions/10849552/update-viewpager-dynamically?rq=1">Update ViewPager dynamically?</a></li>
  <li><a href="https://stackoverflow.com/questions/25928732/removing-fragments-from-fragmentstatepageradapter">Removing fragments from FragmentStatePagerAdapter</a></li>
  </ul>
  
  <p>I have tried various solutions (including the invalidation with <code>POSITION_NONE</code>) 
  . But I still donT know how to remove an item properly. </p>
  
  <p>What happens is </p>
  
  <ul>
  <li>either I get a blank page (meaning the fragment is destroyed, but the
  <code>instantiateItem</code> was not called for a replacement) </li>
  <li>or the whole thing crashes probably because the way the Android manages the
  fragment instances do not match how I keep them in my
  <code>arraylist/sparsearray</code></li>
  </ul>
  
  <p>Here s my <strong>adapter</strong></p>
  
  <pre><code>private class DatePickerPagerAdapter extends FragmentStatePagerAdapter {
  
      ArrayList&lt;Fragment&gt; registeredFragments = new ArrayList&lt;Fragment&gt;();
  
      public DatePickerPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
          return CreateWishFormDatePaginationFragment.newInstance(position);
      }
  
      @Override
      public int getItemPosition(Object object){ //doesnt change much.. 
          return PagerAdapter.POSITION_NONE;
      }
  
      @Override
      public int getCount() {
          return iPageCount;
      }
  
      @Override
      public Object instantiateItem(ViewGroup container, int position) {
          Fragment fragment = (Fragment) super.instantiateItem(container, position);
          registeredFragments.add(position, fragment);
          return fragment;
      }
  
      @Override
      public void destroyItem(ViewGroup container, int position, Object object) {
          super.destroyItem(container, position, registeredFragments.get(position));
      }
  
      public void removePage(ViewGroup pager, int position) {
          destroyItem(pager, position, null);
          registeredFragments.remove(position);
          iPageCount--;
          pagerIndicator.notifyDataSetChanged();
          pagerAdapter.notifyDataSetChanged();
      }
  
      public void addPage() {
  
          iPageCount++;
          pagerIndicator.notifyDataSetChanged();
          pagerAdapter.notifyDataSetChanged();
      }
  }
  </code></pre>
  
  <p>I am using a view pager with ViewPagerIndicator and I want to be able to remove a page in between, for example. </p>
  
  <p>Hence remains the question, <strong>what is the proper way handling addition and removal of fragments in a ViewPager?</strong></p>
  
  <p>Thanks!</p>