title: Removing fragments from FragmentStatePagerAdapter
body: <p><strong>UPDATE</strong></p>
  
  <p>After some major fighting with this problem and help from SO users I managed to solve it.
  This is how my <code>clearProgressUpTo</code> looks like now.</p>
  
  <pre><code>    public void clearProgressUpTo(int progress) {
      boolean deleted = false;
  
      for (int i = fragments.size() - 1; i &gt; 0; i--) {
          int key = fragments.keyAt(i);
          if (key != progress) {
              fragments.delete(key);
              deleted = true;
          } else {
              break;
          }
      }
  
      if (deleted)
          notifyDataSetChanged(); //prevents recursive call (and IllegalStateException: Recursive entry to executePendingTransactions)
  
      while (mSavedState.size() &gt; progress) {
          mSavedState.remove(mSavedState.size()-1);
      }
  
  }
  </code></pre>
  
  <p>The reason why I am doing it after <code>notifyDataSetChanged</code> is because the <code>initiateItem</code> was filling my <code>mSavedState</code> once again. And I am sure that after notifying my <code>adapter</code> does not hold any of these <code>Fragments</code>.</p>
  
  <p>Also if you want to do it like that change your <code>mSaveState</code> to <code>protected</code> so you will be able to get it from the extending class (in my case <code>VerticalAdapter</code>).</p>
  
  <p>I forgot to mention that I am using <a href="http://speakman.net.nz/blog/2014/02/20/a-bug-in-and-a-fix-for-the-way-fragmentstatepageradapter-handles-fragment-restoration/">FragmentStatePagerAdapter fix by Adam Speakman</a> to solve my problem.</p>
  
  <p>/////////////////////////////////////////////////////<strong>QUESTION PART</strong>//////////////////////////////////////////////////////</p>
  
  <p>the problem that I'm having is that I need to remove completely some of <code>Fragments</code> held in <code>FragmentStatePagerAdapter</code>.</p>
  
  <p>I have already done the <code>POSITION_NONE</code> fix on it but the old fragments seems to still remain intact.</p>
  
  <p><strong>CONSTRUCTION</strong></p>
  
  <p>Because the construction of my <code>ViewPager</code> is unusual I was forced to place Inside my <code>VerticalAdater</code> extending <code>FragmentStatePagerAdapter</code> a <code>SparseArray</code> that holds all my fragments.</p>
  
  <pre><code>public class VerticalAdapter extends FragmentStatePagerAdapter {
  
  private SparseArray&lt;Fragment&gt; fragments = new SparseArray&lt;Fragment&gt;(); 
  ...
  
  @Override
  public Fragment getItem(int position) {
      return getFragmentForPosition(position);
  }
  
  @Override
  public int getCount() {
      return fragments.size();
  }
  </code></pre>
  
  <p><strong>PROBLEM</strong></p>
  
  <p>I have made a special method responsible for clearing all the <code>Fragments</code> up to some point in my <code>FragmentStatePagerAdapter</code>. Its working well but the problem I cant get rid of is clearing the <code>ArrayList</code> inside <code>FragmentStatePagerAdapter</code>. Even tho I clear the <code>SparseArray</code>, the <code>ArrayList</code> of <code>FragmentStatePagerAdapter</code> that my <code>VerticalAdapter</code> extends is still holding the <code>Fragments</code> that I don't want to have there. Because of that when I create new <code>Fragments</code> they have the state of the old ones.</p>
  
  <pre><code>public void clearProgressUpTo(int progress) {
      boolean deleted = false;
  
      for (int i = fragments.size() - 1; i &gt; 0; i--) {
          int key = fragments.keyAt(i);
          if (key != progress) {
              fragments.delete(key);
              deleted = true;
          } else {
              break;
          }
      }
  
      if (deleted)
          notifyDataSetChanged(); //prevents recursive call (and IllegalStateException: Recursive entry to executePendingTransactions)
  }
  </code></pre>
  
  <p>If you need more info/code please tell me in the comments I will try to provide as much info as I can. </p>
  
  <p>I have no idea how to fix this problem so any input would be appreciated. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Adding requested code.</p>
  
  <pre><code>private Fragment getFragmentForPosition(int position) {
      return fragments.get(getKeyForPosition(position));
  }
  
  public int getKeyForPosition(int position) {
      int key = 0;
      for (int i = 0; i &lt; fragments.size(); i++) {
          key = fragments.keyAt(i);
          if (i == position) {
              return key;
          }
      }
      return key;
  }
  </code></pre>