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 8703645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:54:51+00:00 2026-06-13T02:54:51+00:00

I’ve got Fragments which can be instantiated in two different ways according to the

  • 0

I’ve got Fragments which can be instantiated in two different ways according to the current layout.
These fragment are loaded in a PageViewer using an Adapter derived from the FragmentStatePageAdapter.
If I run the app just in portrait or landscape mode everything is fine. The fragment are loaded correctly and look ok.
The problem is when I change orientation: The fragment doesn’t change, it seems that it reuse the one loaded for a certain orientation, in fact if I scroll the pager after the next one (wich it was already loaded in the previous orientation) the new fragment are loaded correctly and when I scroll back again even that “starting fragment” is reloaded correctly.

I would like to fix the problem by forcing the recreation of the current and the adjacents fragments but the solution I’ve tried seems to be incomplete/not working.

I’ve overridden the Adapter startUpdate method this way:

    public void startUpdate(ViewGroup container) {
        if(isPortrait != isLastOrientationPortrait){
            container.removeAllViews();
            instantiateItem(container, lastPosition);
        }
        super.startUpdate(container);
    }   

This force the elimination of the fragments from the containers. But I don’t know if it’s a correct approach and how to put the newly instatiated item back to the container.

Any suggestions?

  • 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-13T02:54:52+00:00Added an answer on June 13, 2026 at 2:54 am

    I found a solution to this problem:

    Instead of extends the FragmentStatePagerAdapter class I modified its source code (In case you need it you can find it here: https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/app/FragmentStatePagerAdapter.java)

    What I did was just to modify the restoreStateMethod this way:

    public void restoreState(Parcelable state, ClassLoader loader) {
            Log.d(TAG, "restoreState");
            //We don't want to restore the fragments if the orientation changes!
            if(isLastOrientationPortrait != isPortrait){
                if(state != null){
                    Bundle bundle = (Bundle) state;
                    bundle.setClassLoader(loader);
                    Iterable<String> keys = bundle.keySet();
                    if (mCurTransaction == null) {
                        mCurTransaction = mFragmentManager.beginTransaction();
                    }
                    for (String key : keys) {
                        if (key.startsWith("f")) {
                            Fragment f = mFragmentManager.getFragment(bundle,
                                    key);
                            mCurTransaction.remove(f);
                        }
                    }
                }
                isLastOrientationPortrait = isPortrait;
            }else{
                if (state != null) {
                    Bundle bundle = (Bundle)state;
                    bundle.setClassLoader(loader);
                    Parcelable[] fss = bundle.getParcelableArray("states");
                    mSavedState.clear();
                    mFragments.clear();
                    if (fss != null) {
                        for (int i=0; i<fss.length; i++) {
                            mSavedState.add((Fragment.SavedState)fss[i]);
                        }
                    }
                    Iterable<String> keys = bundle.keySet();
                    for (String key: keys) {
                        if (key.startsWith("f")) {
                            int index = Integer.parseInt(key.substring(1));
                            Fragment f = mFragmentManager.getFragment(bundle, key);
                            if (f != null) {
                                while (mFragments.size() <= index) {
                                    mFragments.add(null);
                                }
                                f.setMenuVisibility(false);
                                mFragments.set(index, f);
                            } else {
                                Log.w(TAG, "Bad fragment at key " + key);
                            }
                        }
                    }
                }
            }
        }
    

    what i basically added is this first if statement:

    if(isLastOrientationPortrait != isPortrait){
                if(state != null){
                    Bundle bundle = (Bundle) state;
                    bundle.setClassLoader(loader);
                    Iterable<String> keys = bundle.keySet();
                    if (mCurTransaction == null) {
                        mCurTransaction = mFragmentManager.beginTransaction();
                    }
                    for (String key : keys) {
                        if (key.startsWith("f")) {
                            Fragment f = mFragmentManager.getFragment(bundle,
                                    key);
                            mCurTransaction.remove(f);
                        }
                    }
                }
                isLastOrientationPortrait = isPortrait;
            }
    

    where isPortrait is the boolean value for the current orientation and isLastOrientationPortrait is the previous orientation saved via the onSaveInstanceState

    This force to discard all the saved fragments and recreate them again on orientation change.

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

Sidebar

Related Questions

I've tracked down a weird MySQL problem to the two different ways I was
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I

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.