UgglyNoodle UgglyNoodle - 13 days ago 7x
Android Question

reorder pages in FragmentStatePagerAdapter using getItemPosition(Object object)

I believe that FragmentStatePagerAdapter does not behave correctly when overriding

getItemPosition(Object object)
with the purpose of reordering the pages.

Below is a simple example. In the initial state, the order of the pages is {A, B, C}. Upon calling
, the order of the pages changes to {A, C, B}. By overriding
getItemPosition(Object object)
, we ensure that the current page being viewed (A, B, or C) does not change.

public static class TestPagerAdapter extends FragmentStatePagerAdapter {
private boolean mState = true;

public TestPagerAdapter(FragmentManager fragmentManager) {

public int getCount() {
return 3;

private void toggleState() {
mState = !mState;

private String getLabel(int position) {
switch (position) {
case 0:
return "A";
case 1:
return mState ? "B" : "C";
return mState ? "C" : "B";

public int getItemPosition(Object object) {
String label = ((TestFragment) object).getLabel();
if (label.equals("A")) {
return 0;
} else if (label.equals("B")) {
return mState ? 1 : 2;
} else {
return mState ? 2 : 1;

public CharSequence getPageTitle(int position) {
return getLabel(position);

public Fragment getItem(int position) {
return TestFragment.newInstance(getLabel(position));

I have encountered two separate behaviours which seem incorrect.

  1. If I immediately call
    (while viewing page A, before swiping to any other page), the app crashes.

    java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
    at java.util.ArrayList.throwIndexOutOfBoundsException(
    at java.util.ArrayList.set(
    at android.database.DataSetObservable.notifyChanged(
    at com.ugglynoodle.test.testfragmentstatepageradapter.MainActivity$TestPagerAdapter.toggleState(

    Looking at the source of
    , this would be fixed by first checking the size of
    (as in lines 113-115) before calling
    in line 136.

  2. If I first swipe to page B, then
    is called, page C is created, and
    now has a size of 3 (this will prevent the crash above from happening in a moment). Then I swipe back to page A, and page C is destroyed, as it should be (since it is 2 pages away, and I'm using the default offscreen page limit of 1). Now, I call
    . Page B is now destroyed. However, page C is NOT recreated! This means, when I now swipe to the right, I get an empty page.

First, it would be nice to know whether I'm correct and these are in fact bugs, or whether I'm doing something wrong. If they are bugs, can anyone suggest a workaround (other than debugging and rebuilding the support library myself)? Surely somebody must have overridden
getItemPosition(Object object)
successfully (apart from setting everything to

I am using the current revision (10) of the support library.


Looking at the source of FragmentStatePagerAdapter, I figured out exactly what is going wrong. The FragmentStatePagerAdapter caches the fragments and saved states in ArrayLists: mFragments and mSavedState. But when the fragments are reordered, there's no mechanism for reordering the elements of mFragments and mSavedState. Therefore, the adapter will provide the wrong fragments to the pager.

I've filed an issue for this, and attached a fixed implementation ( to the issue. In the fix, I've added a getItemId() function to FragmentStatePagerAdapter. (This mirrors the reordering implementation in FragmentPagerAdapter.) An array of the itemIds by adapter position is stored at all times. Then, in notifyDataSetChanged(), the adapter checks if the itemIds array has changed. If it has, then mFragments and mSavedState are reordered accordingly. Further modifications can be found in destroyItem(), saveState() and restoreState().

To use this class, getItemPosition() and getItemId() must be implemented consistently with getItem().