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

  • Home
  • SEARCH
  • 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 8116773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:56:07+00:00 2026-06-06T03:56:07+00:00

I’m having a really hard time to recover from configuration changes with ViewPager .

  • 0

I’m having a really hard time to recover from configuration changes with ViewPager.

What I want is save the current pager position so that after a config change the same page will be visible. To do that, I save pager.getCurrentItem() and restore it using pager.setCurrentItem. Just that this doesn’t work: setCurrentItem actually attempts to recreate that page instead of selecting it! And that’s even though the page has already been created.

Now I’m not sure if I missed something when implementing the adapter that’s backing it, but is there a pitfall to setCurrentItem and when ViewPager thinks that it’s fully constructed? Looking at the code of that function though, it makes no sense to me; it seems to always try to recreate a page, except when there are no items at all:

void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int velocity) {
    // these are the only checked that make this method return
    if (mAdapter == null || mAdapter.getCount() <= 0) {
        setScrollingCacheEnabled(false);
        return;
    }
    if (!always && mCurItem == item && mItems.size() != 0) {
        setScrollingCacheEnabled(false);
        return;
    }

    // ... removed some init code

    mCurItem = item;
    populate();

    // ...
}

As you can see, populate() is always called, except when there are either no items at all, or the item you’re trying to select was already selected. What gives? That doesn’t make sense to me.

  • 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-06T03:56:09+00:00Added an answer on June 6, 2026 at 3:56 am

    setCurrentItem actually attempts to recreate that page instead of selecting it! And that’s even though the page has already been created.

    Not exactly, at least in terms of how I read the code.

    As you can see, populate() is always called, except when there are either no items at all, or the item you’re trying to select was already selected.

    populate() will call addNewItem(), which calls instantiateItem() on the PagerAdapter. The FragmentPagerAdapter implementation of PagerAdapter will use an existing fragment if available, otherwise it calls out to getItem() so you create the page yourself. So, if you use setRetainInstance(true) with your page fragments, and you are using FragmentPagerAdapter, they should be reused.

    FragmentStatePagerAdapter seems more complicated, and if you are rolling your own PagerAdapter sans fragments, it is up to you to implement a caching mechanism.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from

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.