From 47c9105abaf924d18467a9b8c3092fa08cb0c972 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Fri, 2 Jun 2023 07:19:36 +0530 Subject: [PATCH] Store/retrieve saved state as a list. --- .../app/FragmentStatePagerAdapterMenuWorkaround.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/androidx/fragment/app/FragmentStatePagerAdapterMenuWorkaround.java b/app/src/main/java/androidx/fragment/app/FragmentStatePagerAdapterMenuWorkaround.java index 9dea6b48ba7..8d03a148604 100644 --- a/app/src/main/java/androidx/fragment/app/FragmentStatePagerAdapterMenuWorkaround.java +++ b/app/src/main/java/androidx/fragment/app/FragmentStatePagerAdapterMenuWorkaround.java @@ -285,7 +285,7 @@ public Parcelable saveState() { Bundle state = null; if (!mSavedState.isEmpty()) { state = new Bundle(); - state.putParcelableArray("states", mSavedState.toArray(new Fragment.SavedState[0])); + state.putParcelableArrayList("states", mSavedState); } for (int i = 0; i < mFragments.size(); i++) { final Fragment f = mFragments.get(i); @@ -312,14 +312,12 @@ public void restoreState(@Nullable final Parcelable state, @Nullable final Class if (state != null) { final Bundle bundle = (Bundle) state; bundle.setClassLoader(loader); - final Parcelable[] fss = BundleCompat.getParcelableArray( - bundle, "states", Parcelable.class); + final var states = BundleCompat.getParcelableArrayList(bundle, "states", + Fragment.SavedState.class); mSavedState.clear(); mFragments.clear(); - if (fss != null) { - for (final Parcelable parcelable : fss) { - mSavedState.add((Fragment.SavedState) parcelable); - } + if (states != null) { + mSavedState.addAll(states); } final Iterable keys = bundle.keySet(); for (final String key : keys) {