From ee76efb74b5886f98cdfebfbefe9b69224e016fb Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Tue, 5 Jun 2012 10:27:40 -0700 Subject: Fix issue #6584942 IllegalStateException: Failure saving state... ...active SuggestFragment{419494f0} has cleared index: -1 There were issues when the same fragment was removed and then added again before completely finishing the remove (such as due to a running animation). Two fixes: - Now when you call FragmentTransaction.replace() and are replacing a fragment with the same fragment, this becomes a no-op, to avoid visual artifacts in the transition and bad states. - When we are moving the fragment state up and it is currently animating away to the INITIALIZED state, we could end up making the fragment inactive as part of the cleanup. In this case it shouldn't be made inactive; we just need to initialize it but keep it active since we are going to continue to use it. Bug: 6584942 Change-Id: I8bfd73f2d8ef8f67b541b3e2525dfa5db6c3bfa5 --- core/java/android/app/BackStackRecord.java | 61 +++++++++++++++++++----------- core/java/android/app/Fragment.java | 2 +- core/java/android/app/FragmentManager.java | 31 ++++++++------- 3 files changed, 56 insertions(+), 38 deletions(-) (limited to 'core') diff --git a/core/java/android/app/BackStackRecord.java b/core/java/android/app/BackStackRecord.java index e9e8e16..96814b7 100644 --- a/core/java/android/app/BackStackRecord.java +++ b/core/java/android/app/BackStackRecord.java @@ -53,7 +53,7 @@ final class BackStackState implements Parcelable { int pos = 0; while (op != null) { mOps[pos++] = op.cmd; - mOps[pos++] = op.fragment.mIndex; + mOps[pos++] = op.fragment != null ? op.fragment.mIndex : -1; mOps[pos++] = op.enterAnim; mOps[pos++] = op.exitAnim; mOps[pos++] = op.popEnterAnim; @@ -99,8 +99,13 @@ final class BackStackState implements Parcelable { op.cmd = mOps[pos++]; if (FragmentManagerImpl.DEBUG) Log.v(FragmentManagerImpl.TAG, "BSE " + bse + " set base fragment #" + mOps[pos]); - Fragment f = fm.mActive.get(mOps[pos++]); - op.fragment = f; + int findex = mOps[pos++]; + if (findex >= 0) { + Fragment f = fm.mActive.get(findex); + op.fragment = f; + } else { + op.fragment = null; + } op.enterAnim = mOps[pos++]; op.exitAnim = mOps[pos++]; op.popEnterAnim = mOps[pos++]; @@ -506,9 +511,11 @@ final class BackStackRecord extends FragmentTransaction implements + " by " + amt); Op op = mHead; while (op != null) { - op.fragment.mBackStackNesting += amt; - if (FragmentManagerImpl.DEBUG) Log.v(TAG, "Bump nesting of " - + op.fragment + " to " + op.fragment.mBackStackNesting); + if (op.fragment != null) { + op.fragment.mBackStackNesting += amt; + if (FragmentManagerImpl.DEBUG) Log.v(TAG, "Bump nesting of " + + op.fragment + " to " + op.fragment.mBackStackNesting); + } if (op.removed != null) { for (int i=op.removed.size()-1; i>=0; i--) { Fragment r = op.removed.get(i); @@ -568,23 +575,29 @@ final class BackStackRecord extends FragmentTransaction implements Fragment old = mManager.mAdded.get(i); if (FragmentManagerImpl.DEBUG) Log.v(TAG, "OP_REPLACE: adding=" + f + " old=" + old); - if (old.mContainerId == f.mContainerId) { - if (op.removed == null) { - op.removed = new ArrayList(); + if (f == null || old.mContainerId == f.mContainerId) { + if (old == f) { + op.fragment = f = null; + } else { + if (op.removed == null) { + op.removed = new ArrayList(); + } + op.removed.add(old); + old.mNextAnim = op.exitAnim; + if (mAddToBackStack) { + old.mBackStackNesting += 1; + if (FragmentManagerImpl.DEBUG) Log.v(TAG, "Bump nesting of " + + old + " to " + old.mBackStackNesting); + } + mManager.removeFragment(old, mTransition, mTransitionStyle); } - op.removed.add(old); - old.mNextAnim = op.exitAnim; - if (mAddToBackStack) { - old.mBackStackNesting += 1; - if (FragmentManagerImpl.DEBUG) Log.v(TAG, "Bump nesting of " - + old + " to " + old.mBackStackNesting); - } - mManager.removeFragment(old, mTransition, mTransitionStyle); } } } - f.mNextAnim = op.enterAnim; - mManager.addFragment(f, false); + if (f != null) { + f.mNextAnim = op.enterAnim; + mManager.addFragment(f, false); + } } break; case OP_REMOVE: { Fragment f = op.fragment; @@ -644,10 +657,12 @@ final class BackStackRecord extends FragmentTransaction implements } break; case OP_REPLACE: { Fragment f = op.fragment; - f.mNextAnim = op.popExitAnim; - mManager.removeFragment(f, - FragmentManagerImpl.reverseTransit(mTransition), - mTransitionStyle); + if (f != null) { + f.mNextAnim = op.popExitAnim; + mManager.removeFragment(f, + FragmentManagerImpl.reverseTransit(mTransition), + mTransitionStyle); + } if (op.removed != null) { for (int i=0; i