Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found a solution to this problem:</p> <p>Instead of extends the <code>FragmentStatePagerAdapter</code> class I modified its source code (In case you need it you can find it here: <a href="https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/app/FragmentStatePagerAdapter.java" rel="nofollow">https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/app/FragmentStatePagerAdapter.java</a>)</p> <p>What I did was just to modify the <code>restoreStateMethod</code> this way:</p> <pre><code>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&lt;String&gt; 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&lt;fss.length; i++) { mSavedState.add((Fragment.SavedState)fss[i]); } } Iterable&lt;String&gt; 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() &lt;= index) { mFragments.add(null); } f.setMenuVisibility(false); mFragments.set(index, f); } else { Log.w(TAG, "Bad fragment at key " + key); } } } } } } </code></pre> <p>what i basically added is this first if statement:</p> <pre><code>if(isLastOrientationPortrait != isPortrait){ if(state != null){ Bundle bundle = (Bundle) state; bundle.setClassLoader(loader); Iterable&lt;String&gt; 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; } </code></pre> <p>where <code>isPortrait</code> is the boolean value for the current orientation and <code>isLastOrientationPortrait</code> is the previous orientation saved via the <code>onSaveInstanceState</code></p> <p>This force to discard all the saved fragments and recreate them again on orientation change.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload