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