From a2674f730895e815865121d531f20a6c9ec5a07d Mon Sep 17 00:00:00 2001 From: Jaewoong Jung Date: Wed, 30 Nov 2016 12:48:18 -0800 Subject: [PATCH] Makes app settings keep scroll position after unistalling apps. Bug: 32195844 Test: Manual inspection after numerous tries to write one. Espresso didn't work because the test scenario involves installing an app. Robolectric didn't either because it doesn't fully emulate ListView's scroll behavior. Change-Id: Ia811198ddee6edf6d207a4e936d2e686b64bf02f --- .../applications/ManageApplications.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/com/android/settings/applications/ManageApplications.java b/src/com/android/settings/applications/ManageApplications.java index 7dee5f41489..0aca5e9eeb9 100644 --- a/src/com/android/settings/applications/ManageApplications.java +++ b/src/com/android/settings/applications/ManageApplications.java @@ -767,6 +767,12 @@ public class ManageApplications extends InstrumentedFragment private boolean mHasReceivedLoadEntries; private boolean mHasReceivedBridgeCallback; + // These two variables are used to remember and restore the last scroll position when this + // fragment is paused. We need this special handling because app entries are added gradually + // when we rebuild the list after the user made some changes, like uninstalling an app. + private int mLastIndex = -1; + private int mLastTop; + private AlphabeticIndex.ImmutableIndex mIndex; private SectionInfo[] mSections = EMPTY_SECTIONS; private int[] mPositionToSectionIndex; @@ -851,6 +857,10 @@ public class ManageApplications extends InstrumentedFragment mExtraInfoBridge.pause(); } } + // Record the current scroll position before pausing. + mLastIndex = mManageApplications.mListView.getFirstVisiblePosition(); + View v = mManageApplications.mListView.getChildAt(0); + mLastTop = (v == null) ? 0 : (v.getTop() - mManageApplications.mListView.getPaddingTop()); } public void release() { @@ -971,6 +981,12 @@ public class ManageApplications extends InstrumentedFragment } notifyDataSetChanged(); + // Restore the last scroll position if the number of entries added so far is bigger than + // it. + if (mLastIndex != -1 && getCount() > mLastIndex) { + mManageApplications.mListView.setSelectionFromTop(mLastIndex, mLastTop); + mLastIndex = -1; + } if (mSession.getAllApps().size() != 0 && mManageApplications.mListContainer.getVisibility() != View.VISIBLE) {