@Test public void testSaveStateOnEmptyAdapter() { final FragmentManager mockFragmentManager = mock(FragmentManager.class); final FragmentStatePagerAdapter adapter = new Adapter(mockFragmentManager); assertThat(adapter.saveState(), is(nullValue())); }
@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; }