Skip to content

Commit e181bd9

Browse files
author
Dianne Hackborn
committed
Fix AbsListView to correctly retain its state if not layed out.
This covers a hole where if the list view restores its state and then is asked to save its state before its layout happens, the original state is lost. Instead we just store that original state. Also tweak FragmentManager to make sure an inactive fragment can not have its state moved up out of CREATED. Bug #7232088: ListView saved state being lost in some cases Change-Id: I5b40f37c259c7bcbe17dd1330016f9531f1b5534
1 parent b1c4ab5 commit e181bd9

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

core/java/android/app/FragmentManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ void moveToState(Fragment f, int newState, int transit, int transitionStyle,
776776
// + " mRemoving=" + f.mRemoving + " Callers=" + Debug.getCallers(5));
777777

778778
// Fragments that are not currently added will sit in the onCreate() state.
779-
if (!f.mAdded && newState > Fragment.CREATED) {
779+
if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) {
780780
newState = Fragment.CREATED;
781781
}
782782
if (f.mRemoving && newState > f.mState) {

core/java/android/widget/AbsListView.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,14 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
674674
*/
675675
static final Interpolator sLinearInterpolator = new LinearInterpolator();
676676

677+
/**
678+
* The saved state that we will be restoring from when we next sync.
679+
* Kept here so that if we happen to be asked to save our state before
680+
* the sync happens, we can return this existing data rather than losing
681+
* it.
682+
*/
683+
private SavedState mPendingSync;
684+
677685
/**
678686
* Interface definition for a callback to be invoked when the list or grid
679687
* has been scrolled.
@@ -1612,6 +1620,21 @@ public Parcelable onSaveInstanceState() {
16121620

16131621
SavedState ss = new SavedState(superState);
16141622

1623+
if (mPendingSync != null) {
1624+
// Just keep what we last restored.
1625+
ss.selectedId = mPendingSync.selectedId;
1626+
ss.firstId = mPendingSync.firstId;
1627+
ss.viewTop = mPendingSync.viewTop;
1628+
ss.position = mPendingSync.position;
1629+
ss.height = mPendingSync.height;
1630+
ss.filter = mPendingSync.filter;
1631+
ss.inActionMode = mPendingSync.inActionMode;
1632+
ss.checkedItemCount = mPendingSync.checkedItemCount;
1633+
ss.checkState = mPendingSync.checkState;
1634+
ss.checkIdState = mPendingSync.checkIdState;
1635+
return ss;
1636+
}
1637+
16151638
boolean haveChildren = getChildCount() > 0 && mItemCount > 0;
16161639
long selectedId = getSelectedItemId();
16171640
ss.selectedId = selectedId;
@@ -1692,6 +1715,7 @@ public void onRestoreInstanceState(Parcelable state) {
16921715

16931716
if (ss.selectedId >= 0) {
16941717
mNeedSync = true;
1718+
mPendingSync = ss;
16951719
mSyncRowId = ss.selectedId;
16961720
mSyncPosition = ss.position;
16971721
mSpecificTop = ss.viewTop;
@@ -1702,6 +1726,7 @@ public void onRestoreInstanceState(Parcelable state) {
17021726
setNextSelectedPositionInt(INVALID_POSITION);
17031727
mSelectorPosition = INVALID_POSITION;
17041728
mNeedSync = true;
1729+
mPendingSync = ss;
17051730
mSyncRowId = ss.firstId;
17061731
mSyncPosition = ss.position;
17071732
mSpecificTop = ss.viewTop;
@@ -1803,6 +1828,7 @@ void resetList() {
18031828
mDataChanged = false;
18041829
mPositionScrollAfterLayout = null;
18051830
mNeedSync = false;
1831+
mPendingSync = null;
18061832
mOldSelectedPosition = INVALID_POSITION;
18071833
mOldSelectedRowId = INVALID_ROW_ID;
18081834
setSelectedPositionInt(INVALID_POSITION);
@@ -5209,6 +5235,7 @@ protected void handleDataChanged() {
52095235
if (mNeedSync) {
52105236
// Update this first, since setNextSelectedPositionInt inspects it
52115237
mNeedSync = false;
5238+
mPendingSync = null;
52125239

52135240
if (mTranscriptMode == TRANSCRIPT_MODE_ALWAYS_SCROLL) {
52145241
mLayoutMode = LAYOUT_FORCE_BOTTOM;
@@ -5324,6 +5351,7 @@ protected void handleDataChanged() {
53245351
mNextSelectedPosition = INVALID_POSITION;
53255352
mNextSelectedRowId = INVALID_ROW_ID;
53265353
mNeedSync = false;
5354+
mPendingSync = null;
53275355
mSelectorPosition = INVALID_POSITION;
53285356
checkSelectionChanged();
53295357
}

0 commit comments

Comments
 (0)