@Test public void testRestoreNullState() { final FragmentManager mockFragmentManager = mock(FragmentManager.class); final FragmentStatePagerAdapter adapter = new Adapter(mockFragmentManager); adapter.restoreState(null, Adapter.class.getClassLoader()); }
@Override public void restoreState(@Nullable final Parcelable state, @Nullable final ClassLoader loader) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; bundle.setClassLoader(loader); final long[] savedStateIds = bundle.getLongArray(STATE_FRAGMENT_STATES); this.savedStates.clear(); this.fragments.clear(); if (savedStateIds != null && savedStateIds.length > 0) { for (final long itemId : savedStateIds) { this.savedStates.put(itemId, bundle.<Fragment.SavedState>getParcelable("android:pager:fragment_state:" + itemId)); } } final Iterable<String> keys = bundle.keySet(); for (final String key : keys) { if (key.startsWith("android:pager:fragment:")) { final Fragment fragment = fragmentManager.getFragment(bundle, key); if (fragment == null) { PagerAdaptersLogging.w(TAG, "Fragment not found for the key " + key + " when restoring adapter's state."); } else { fragment.setMenuVisibility(false); this.fragments.put(Long.parseLong(key.split(":")[3]), fragment); } } } } }
FragmentStatePagerAdapter extends PagerAdapter { @Override public void restoreState(@Nullable final Parcelable state, @Nullable final ClassLoader loader) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; bundle.setClassLoader(loader); final long[] savedStateIds = bundle.getLongArray(STATE_FRAGMENT_STATES); this.savedStates.clear(); this.fragments.clear(); if (savedStateIds != null && savedStateIds.length > 0) { for (final long itemId : savedStateIds) { this.savedStates.put(itemId, bundle.<Fragment.SavedState>getParcelable("android:pager:fragment_state:" + itemId)); } } final Iterable<String> keys = bundle.keySet(); for (final String key : keys) { if (key.startsWith("android:pager:fragment:")) { final Fragment fragment = fragmentManager.getFragment(bundle, key); if (fragment == null) { PagerAdaptersLogging.w(TAG, "Fragment not found for the key " + key + " when restoring adapter's state."); } else { fragment.setMenuVisibility(false); this.fragments.put(Long.parseLong(key.split(":")[3]), fragment); } } } } } }
FragmentStatePagerAdapter extends PagerAdapter { @Override public void restoreState(@Nullable final Parcelable state, @Nullable final ClassLoader loader) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; bundle.setClassLoader(loader); final long[] savedStateIds = bundle.getLongArray(STATE_FRAGMENT_STATES); this.savedStates.clear(); this.fragments.clear(); if (savedStateIds != null && savedStateIds.length > 0) { for (final long itemId : savedStateIds) { this.savedStates.put(itemId, bundle.<Fragment.SavedState>getParcelable("android:pager:fragment_state:" + itemId)); } } final Iterable<String> keys = bundle.keySet(); for (final String key : keys) { if (key.startsWith("android:pager:fragment:")) { final Fragment fragment = fragmentManager.getFragment(bundle, key); if (fragment == null) { PagerAdaptersLogging.w(TAG, "Fragment not found for the key " + key + " when restoring adapter's state."); } else { fragment.setMenuVisibility(false); this.fragments.put(Long.parseLong(key.split(":")[3]), fragment); } } } } } FragmentStatePagerAdapter(@NonNull final FragmentManager fragmentManager); }
FragmentStatePagerAdapter extends PagerAdapter { @Override public void restoreState(@Nullable final Parcelable state, @Nullable final ClassLoader loader) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; bundle.setClassLoader(loader); final long[] savedStateIds = bundle.getLongArray(STATE_FRAGMENT_STATES); this.savedStates.clear(); this.fragments.clear(); if (savedStateIds != null && savedStateIds.length > 0) { for (final long itemId : savedStateIds) { this.savedStates.put(itemId, bundle.<Fragment.SavedState>getParcelable("android:pager:fragment_state:" + itemId)); } } final Iterable<String> keys = bundle.keySet(); for (final String key : keys) { if (key.startsWith("android:pager:fragment:")) { final Fragment fragment = fragmentManager.getFragment(bundle, key); if (fragment == null) { PagerAdaptersLogging.w(TAG, "Fragment not found for the key " + key + " when restoring adapter's state."); } else { fragment.setMenuVisibility(false); this.fragments.put(Long.parseLong(key.split(":")[3]), fragment); } } } } } 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 void restoreState(@Nullable final Parcelable state, @Nullable final ClassLoader loader) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; bundle.setClassLoader(loader); final long[] savedStateIds = bundle.getLongArray(STATE_FRAGMENT_STATES); this.savedStates.clear(); this.fragments.clear(); if (savedStateIds != null && savedStateIds.length > 0) { for (final long itemId : savedStateIds) { this.savedStates.put(itemId, bundle.<Fragment.SavedState>getParcelable("android:pager:fragment_state:" + itemId)); } } final Iterable<String> keys = bundle.keySet(); for (final String key : keys) { if (key.startsWith("android:pager:fragment:")) { final Fragment fragment = fragmentManager.getFragment(bundle, key); if (fragment == null) { PagerAdaptersLogging.w(TAG, "Fragment not found for the key " + key + " when restoring adapter's state."); } else { fragment.setMenuVisibility(false); this.fragments.put(Long.parseLong(key.split(":")[3]), fragment); } } } } } 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; }