@SuppressWarnings("ConstantConditions") @Test public void testSaveState() { final FragmentManager mockFragmentManager = mock(FragmentManager.class); final Adapter adapter = new Adapter(mockFragmentManager); final int mockCount = 5; final Bundle restoreState = new Bundle(); final long[] mockFragmentStateIds = new long[mockCount]; final Fragment.SavedState[] mockFragmentStates = new Fragment.SavedState[mockCount]; for (int i = 0; i < mockCount; i++) { final long itemId = adapter.getItemId(i); mockFragmentStateIds[i] = itemId; mockFragmentStates[i] = mock(Fragment.SavedState.class); restoreState.putParcelable("android:pager:fragment_state:" + itemId, mockFragmentStates[i]); } restoreState.putLongArray(FragmentStatePagerAdapter.STATE_FRAGMENT_STATES, mockFragmentStateIds); adapter.restoreState(restoreState, Adapter.class.getClassLoader()); final FragmentActivity activity = Robolectric.buildActivity(TestCompatActivity.class).create().start().resume().get(); final ViewGroup activityContentView = activity.findViewById(TestActivity.CONTENT_VIEW_ID); final FragmentManager fragmentManager = activity.getSupportFragmentManager(); adapter.setFragmentFactory(new FragmentFactory() { @Override @NonNull public Fragment createFragment(final int position) { return new TestFragment(); } }); when(mockFragmentManager.beginTransaction()).thenReturn(fragmentManager.beginTransaction()); final Fragment[] fragments = new Fragment[mockCount]; for (int i = 0; i < mockCount; i++) { fragments[i] = (Fragment) adapter.instantiateItem(activityContentView, i); } adapter.finishUpdate(activityContentView); fragmentManager.beginTransaction().remove(fragments[0]).commitAllowingStateLoss(); fragmentManager.executePendingTransactions(); final Parcelable state = adapter.saveState(); assertThat(state, is(notNullValue())); assertThat(state, instanceOf(Bundle.class)); final Bundle savedState = (Bundle) state; final long[] savedStateIds = savedState.getLongArray(FragmentStatePagerAdapter.STATE_FRAGMENT_STATES); assertThat(savedStateIds, is(notNullValue())); assertThat(savedStateIds.length, is(mockCount)); for (int i = 0; i < mockCount; i++) { final long itemId = adapter.getItemId(i); assertThat(savedStateIds[i], is(itemId)); assertThat(savedState.getParcelable("android:pager:fragment_state:" + itemId), Is.<Parcelable>is(mockFragmentStates[i])); if (i == 0) { verify(mockFragmentManager, times(0)).putFragment(savedState, "android:pager:fragment:" + itemId, fragments[i]); } else { verify(mockFragmentManager, times(1)).putFragment(savedState, "android:pager:fragment:" + itemId, fragments[i]); } } }
@Override public Parcelable saveState() { Bundle state = null; final int statesCount = savedStates.size(); if (statesCount > 0) { state = new Bundle(); final long[] savedStateIds = new long[statesCount]; for (int i = 0; i < statesCount; i++) { final long itemId = savedStates.keyAt(i); savedStateIds[i] = itemId; state.putParcelable("android:pager:fragment_state:" + itemId, savedStates.valueAt(i)); } state.putLongArray(STATE_FRAGMENT_STATES, savedStateIds); } for (int i = 0; i < fragments.size(); i++) { final long itemId = fragments.keyAt(i); final Fragment fragment = fragments.get(itemId); if (fragment.isAdded()) { if (state == null) { state = new Bundle(); } this.fragmentManager.putFragment(state, "android:pager:fragment:" + itemId, fragment); } } return state; }
FragmentStatePagerAdapter extends PagerAdapter { @Override public Parcelable saveState() { Bundle state = null; final int statesCount = savedStates.size(); if (statesCount > 0) { state = new Bundle(); final long[] savedStateIds = new long[statesCount]; for (int i = 0; i < statesCount; i++) { final long itemId = savedStates.keyAt(i); savedStateIds[i] = itemId; state.putParcelable("android:pager:fragment_state:" + itemId, savedStates.valueAt(i)); } state.putLongArray(STATE_FRAGMENT_STATES, savedStateIds); } for (int i = 0; i < fragments.size(); i++) { final long itemId = fragments.keyAt(i); final Fragment fragment = fragments.get(itemId); if (fragment.isAdded()) { if (state == null) { state = new Bundle(); } this.fragmentManager.putFragment(state, "android:pager:fragment:" + itemId, fragment); } } return state; } }
FragmentStatePagerAdapter extends PagerAdapter { @Override public Parcelable saveState() { Bundle state = null; final int statesCount = savedStates.size(); if (statesCount > 0) { state = new Bundle(); final long[] savedStateIds = new long[statesCount]; for (int i = 0; i < statesCount; i++) { final long itemId = savedStates.keyAt(i); savedStateIds[i] = itemId; state.putParcelable("android:pager:fragment_state:" + itemId, savedStates.valueAt(i)); } state.putLongArray(STATE_FRAGMENT_STATES, savedStateIds); } for (int i = 0; i < fragments.size(); i++) { final long itemId = fragments.keyAt(i); final Fragment fragment = fragments.get(itemId); if (fragment.isAdded()) { if (state == null) { state = new Bundle(); } this.fragmentManager.putFragment(state, "android:pager:fragment:" + itemId, fragment); } } return state; } FragmentStatePagerAdapter(@NonNull final FragmentManager fragmentManager); }
FragmentStatePagerAdapter extends PagerAdapter { @Override public Parcelable saveState() { Bundle state = null; final int statesCount = savedStates.size(); if (statesCount > 0) { state = new Bundle(); final long[] savedStateIds = new long[statesCount]; for (int i = 0; i < statesCount; i++) { final long itemId = savedStates.keyAt(i); savedStateIds[i] = itemId; state.putParcelable("android:pager:fragment_state:" + itemId, savedStates.valueAt(i)); } state.putLongArray(STATE_FRAGMENT_STATES, savedStateIds); } for (int i = 0; i < fragments.size(); i++) { final long itemId = fragments.keyAt(i); final Fragment fragment = fragments.get(itemId); if (fragment.isAdded()) { if (state == null) { state = new Bundle(); } this.fragmentManager.putFragment(state, "android:pager:fragment:" + itemId, fragment); } } return state; } FragmentStatePagerAdapter(@NonNull final FragmentManager fragmentManager); @Override void startUpdate(@NonNull final ViewGroup container); @SuppressLint("CommitTransaction") @Override @NonNull Object instantiateItem(@NonNull final ViewGroup container, final int position); long getItemId(final int position); @NonNull abstract Fragment getItem(int position); @Override void setPrimaryItem(@NonNull final ViewGroup container, final int position, @Nullable final Object object); @Nullable Fragment getPrimaryFragment(); @IntRange(from = NO_POSITION) int getPrimaryPosition(); @Override boolean isViewFromObject(@NonNull final View view, @NonNull final Object object); @SuppressLint("CommitTransaction") @Override void destroyItem(@NonNull final ViewGroup container, final int position, @NonNull final Object object); @Override void finishUpdate(@NonNull final ViewGroup container); @Override Parcelable saveState(); @Override void restoreState(@Nullable final Parcelable state, @Nullable final ClassLoader loader); }
FragmentStatePagerAdapter extends PagerAdapter { @Override public Parcelable saveState() { Bundle state = null; final int statesCount = savedStates.size(); if (statesCount > 0) { state = new Bundle(); final long[] savedStateIds = new long[statesCount]; for (int i = 0; i < statesCount; i++) { final long itemId = savedStates.keyAt(i); savedStateIds[i] = itemId; state.putParcelable("android:pager:fragment_state:" + itemId, savedStates.valueAt(i)); } state.putLongArray(STATE_FRAGMENT_STATES, savedStateIds); } for (int i = 0; i < fragments.size(); i++) { final long itemId = fragments.keyAt(i); final Fragment fragment = fragments.get(itemId); if (fragment.isAdded()) { if (state == null) { state = new Bundle(); } this.fragmentManager.putFragment(state, "android:pager:fragment:" + itemId, fragment); } } return state; } FragmentStatePagerAdapter(@NonNull final FragmentManager fragmentManager); @Override void startUpdate(@NonNull final ViewGroup container); @SuppressLint("CommitTransaction") @Override @NonNull Object instantiateItem(@NonNull final ViewGroup container, final int position); long getItemId(final int position); @NonNull abstract Fragment getItem(int position); @Override void setPrimaryItem(@NonNull final ViewGroup container, final int position, @Nullable final Object object); @Nullable Fragment getPrimaryFragment(); @IntRange(from = NO_POSITION) int getPrimaryPosition(); @Override boolean isViewFromObject(@NonNull final View view, @NonNull final Object object); @SuppressLint("CommitTransaction") @Override void destroyItem(@NonNull final ViewGroup container, final int position, @NonNull final Object object); @Override void finishUpdate(@NonNull final ViewGroup container); @Override Parcelable saveState(); @Override void restoreState(@Nullable final Parcelable state, @Nullable final ClassLoader loader); static final int NO_POSITION; static final long NO_ID; }