Merge pull request #7149 from TacoTheDank/updateFragWorkaround

Update pager workaround code to Fragment 1.3.6
This commit is contained in:
litetex 2021-10-20 19:44:35 +02:00 committed by GitHub
commit e0b1a6b88b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -51,8 +51,12 @@ import java.util.ArrayList;
* <li>{@link #saveState()}</li> * <li>{@link #saveState()}</li>
* <li>{@link #restoreState(Parcelable, ClassLoader)}</li> * <li>{@link #restoreState(Parcelable, ClassLoader)}</li>
* </ul> * </ul>
*
* @deprecated Switch to {@link androidx.viewpager2.widget.ViewPager2} and use
* {@link androidx.viewpager2.adapter.FragmentStateAdapter} instead.
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Deprecated
public abstract class FragmentStatePagerAdapterMenuWorkaround extends PagerAdapter { public abstract class FragmentStatePagerAdapterMenuWorkaround extends PagerAdapter {
private static final String TAG = "FragmentStatePagerAdapt"; private static final String TAG = "FragmentStatePagerAdapt";
private static final boolean DEBUG = false; private static final boolean DEBUG = false;
@ -86,9 +90,10 @@ public abstract class FragmentStatePagerAdapterMenuWorkaround extends PagerAdapt
private final int mBehavior; private final int mBehavior;
private FragmentTransaction mCurTransaction = null; private FragmentTransaction mCurTransaction = null;
private final ArrayList<Fragment.SavedState> mSavedState = new ArrayList<Fragment.SavedState>(); private final ArrayList<Fragment.SavedState> mSavedState = new ArrayList<>();
private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>(); private final ArrayList<Fragment> mFragments = new ArrayList<>();
private Fragment mCurrentPrimaryItem = null; private Fragment mCurrentPrimaryItem = null;
private boolean mExecutingFinishUpdate;
/** /**
* Constructor for {@link FragmentStatePagerAdapterMenuWorkaround} * Constructor for {@link FragmentStatePagerAdapterMenuWorkaround}
@ -208,7 +213,7 @@ public abstract class FragmentStatePagerAdapterMenuWorkaround extends PagerAdapt
mFragments.set(position, null); mFragments.set(position, null);
mCurTransaction.remove(fragment); mCurTransaction.remove(fragment);
if (fragment == mCurrentPrimaryItem) { if (fragment.equals(mCurrentPrimaryItem)) {
mCurrentPrimaryItem = null; mCurrentPrimaryItem = null;
} }
} }
@ -247,7 +252,19 @@ public abstract class FragmentStatePagerAdapterMenuWorkaround extends PagerAdapt
@Override @Override
public void finishUpdate(@NonNull final ViewGroup container) { public void finishUpdate(@NonNull final ViewGroup container) {
if (mCurTransaction != null) { if (mCurTransaction != null) {
// We drop any transactions that attempt to be committed
// from a re-entrant call to finishUpdate(). We need to
// do this as a workaround for Robolectric running measure/layout
// calls inline rather than allowing them to be posted
// as they would on a real device.
if (!mExecutingFinishUpdate) {
try {
mExecutingFinishUpdate = true;
mCurTransaction.commitNowAllowingStateLoss(); mCurTransaction.commitNowAllowingStateLoss();
} finally {
mExecutingFinishUpdate = false;
}
}
mCurTransaction = null; mCurTransaction = null;
} }
} }