Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e181bd9b authored by Dianne Hackborn's avatar Dianne Hackborn
Browse files

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
parent b1c4ab5c
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -776,7 +776,7 @@ final class FragmentManagerImpl extends FragmentManager {
        //    + " mRemoving=" + f.mRemoving + " Callers=" + Debug.getCallers(5));
        //    + " mRemoving=" + f.mRemoving + " Callers=" + Debug.getCallers(5));


        // Fragments that are not currently added will sit in the onCreate() state.
        // Fragments that are not currently added will sit in the onCreate() state.
        if (!f.mAdded && newState > Fragment.CREATED) {
        if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) {
            newState = Fragment.CREATED;
            newState = Fragment.CREATED;
        }
        }
        if (f.mRemoving && newState > f.mState) {
        if (f.mRemoving && newState > f.mState) {
+28 −0
Original line number Original line Diff line number Diff line
@@ -674,6 +674,14 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
     */
     */
    static final Interpolator sLinearInterpolator = new LinearInterpolator();
    static final Interpolator sLinearInterpolator = new LinearInterpolator();


    /**
     * The saved state that we will be restoring from when we next sync.
     * Kept here so that if we happen to be asked to save our state before
     * the sync happens, we can return this existing data rather than losing
     * it.
     */
    private SavedState mPendingSync;

    /**
    /**
     * Interface definition for a callback to be invoked when the list or grid
     * Interface definition for a callback to be invoked when the list or grid
     * has been scrolled.
     * has been scrolled.
@@ -1612,6 +1620,21 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te


        SavedState ss = new SavedState(superState);
        SavedState ss = new SavedState(superState);


        if (mPendingSync != null) {
            // Just keep what we last restored.
            ss.selectedId = mPendingSync.selectedId;
            ss.firstId = mPendingSync.firstId;
            ss.viewTop = mPendingSync.viewTop;
            ss.position = mPendingSync.position;
            ss.height = mPendingSync.height;
            ss.filter = mPendingSync.filter;
            ss.inActionMode = mPendingSync.inActionMode;
            ss.checkedItemCount = mPendingSync.checkedItemCount;
            ss.checkState = mPendingSync.checkState;
            ss.checkIdState = mPendingSync.checkIdState;
            return ss;
        }

        boolean haveChildren = getChildCount() > 0 && mItemCount > 0;
        boolean haveChildren = getChildCount() > 0 && mItemCount > 0;
        long selectedId = getSelectedItemId();
        long selectedId = getSelectedItemId();
        ss.selectedId = selectedId;
        ss.selectedId = selectedId;
@@ -1692,6 +1715,7 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te


        if (ss.selectedId >= 0) {
        if (ss.selectedId >= 0) {
            mNeedSync = true;
            mNeedSync = true;
            mPendingSync = ss;
            mSyncRowId = ss.selectedId;
            mSyncRowId = ss.selectedId;
            mSyncPosition = ss.position;
            mSyncPosition = ss.position;
            mSpecificTop = ss.viewTop;
            mSpecificTop = ss.viewTop;
@@ -1702,6 +1726,7 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
            setNextSelectedPositionInt(INVALID_POSITION);
            setNextSelectedPositionInt(INVALID_POSITION);
            mSelectorPosition = INVALID_POSITION;
            mSelectorPosition = INVALID_POSITION;
            mNeedSync = true;
            mNeedSync = true;
            mPendingSync = ss;
            mSyncRowId = ss.firstId;
            mSyncRowId = ss.firstId;
            mSyncPosition = ss.position;
            mSyncPosition = ss.position;
            mSpecificTop = ss.viewTop;
            mSpecificTop = ss.viewTop;
@@ -1803,6 +1828,7 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
        mDataChanged = false;
        mDataChanged = false;
        mPositionScrollAfterLayout = null;
        mPositionScrollAfterLayout = null;
        mNeedSync = false;
        mNeedSync = false;
        mPendingSync = null;
        mOldSelectedPosition = INVALID_POSITION;
        mOldSelectedPosition = INVALID_POSITION;
        mOldSelectedRowId = INVALID_ROW_ID;
        mOldSelectedRowId = INVALID_ROW_ID;
        setSelectedPositionInt(INVALID_POSITION);
        setSelectedPositionInt(INVALID_POSITION);
@@ -5209,6 +5235,7 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
            if (mNeedSync) {
            if (mNeedSync) {
                // Update this first, since setNextSelectedPositionInt inspects it
                // Update this first, since setNextSelectedPositionInt inspects it
                mNeedSync = false;
                mNeedSync = false;
                mPendingSync = null;


                if (mTranscriptMode == TRANSCRIPT_MODE_ALWAYS_SCROLL) {
                if (mTranscriptMode == TRANSCRIPT_MODE_ALWAYS_SCROLL) {
                    mLayoutMode = LAYOUT_FORCE_BOTTOM;
                    mLayoutMode = LAYOUT_FORCE_BOTTOM;
@@ -5324,6 +5351,7 @@ public abstract class AbsListView extends AdapterView<ListAdapter> implements Te
        mNextSelectedPosition = INVALID_POSITION;
        mNextSelectedPosition = INVALID_POSITION;
        mNextSelectedRowId = INVALID_ROW_ID;
        mNextSelectedRowId = INVALID_ROW_ID;
        mNeedSync = false;
        mNeedSync = false;
        mPendingSync = null;
        mSelectorPosition = INVALID_POSITION;
        mSelectorPosition = INVALID_POSITION;
        checkSelectionChanged();
        checkSelectionChanged();
    }
    }