Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:50:06+00:00 2026-06-12T02:50:06+00:00

I believe that FragmentStatePagerAdapter does not behave correctly when overriding getItemPosition(Object object) with the

  • 0

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 toggleState(), 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) {
        super(fragmentManager);
    }

    @Override
    public int getCount() {
        return 3;
    }

    private void toggleState() {
        mState = !mState;
        notifyDataSetChanged();
    }

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

    @Override
    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;
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return getLabel(position);
    }

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

I have encountered two separate behaviours which seem incorrect.

  1. If I immediately call toggleState() (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(ArrayList.java:251)
      at java.util.ArrayList.set(ArrayList.java:477)
      at android.support.v4.app.FragmentStatePagerAdapter.destroyItem(FragmentStatePagerAdapter.java:136)
      at android.support.v4.view.ViewPager.populate(ViewPager.java:867)
      at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:469)
      at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:441)
      at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:766)
      at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2519)
      at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
      at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
      at com.ugglynoodle.test.testfragmentstatepageradapter.MainActivity$TestPagerAdapter.toggleState(MainActivity.java:55)
      ...
    

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

  2. If I first swipe to page B, then getItem(2) is called, page C is created, and mFragments 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 toggleState(). 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 POSITION_NONE)?

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:50:08+00:00Added an answer on June 12, 2026 at 2:50 am

    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 (NewFragmentStatePagerAdapter.java) 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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I believe that the C++ standard for std::sort does not guarantee O(n) performance on
I believe that the rallytagpicker is definitely a useful component. However, I do not
I used to believe that you should not insert javascript blocks <script language=javascript> <!--
I'm not sure but I believe that Python is -next to Objective-C- somewhat natural
I do not believe that it is possible to completely avoid C-style casts when
I believe that the little icon in the title bar in the image below
My understanding of Dart leads me to believe that this 'cast' should not affect
I believe that local integer variables are not initialized to zero in delphi. The
I believe that rails 3 support for windows is not released yet. Can I
NOTE: I do believe that this is not an openCV related problem but since

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.