@Test public void testDestroyItemNotInstantiated() { final FragmentManager mockFragmentManager = mock(FragmentManager.class); final FragmentStatePagerAdapter adapter = new Adapter(mockFragmentManager); final FragmentTransaction mockTransaction = mock(FragmentTransaction.class); when(mockFragmentManager.beginTransaction()).thenReturn(mockTransaction); final Fragment mockFragment = mock(Fragment.class); adapter.destroyItem(container, 0, mockFragment); verify(mockTransaction).remove(mockFragment); verifyNoMoreInteractions(mockTransaction); }
@SuppressLint("CommitTransaction") @Override public void destroyItem(@NonNull final ViewGroup container, final int position, @NonNull final Object object) { if (pendingTransaction == null) { this.pendingTransaction = fragmentManager.beginTransaction(); } if (position == primaryPosition) { this.primaryPosition = NO_POSITION; this.primaryItem = null; } final Fragment fragment = (Fragment) object; final int itemIndex = fragments.indexOfValue(fragment); if (itemIndex >= 0) { final long itemId = fragments.keyAt(itemIndex); PagerAdaptersLogging.d(TAG, "Removing item(id: " + itemId + ", fragment: " + fragment + ", view: " + fragment.getView() + ")."); if (fragment.isAdded()) { this.savedStates.put(itemId, fragmentManager.saveFragmentInstanceState(fragment)); } else { this.savedStates.remove(itemId); } this.fragments.remove(itemId); } this.pendingTransaction.remove(fragment); }
FragmentStatePagerAdapter extends PagerAdapter { @SuppressLint("CommitTransaction") @Override public void destroyItem(@NonNull final ViewGroup container, final int position, @NonNull final Object object) { if (pendingTransaction == null) { this.pendingTransaction = fragmentManager.beginTransaction(); } if (position == primaryPosition) { this.primaryPosition = NO_POSITION; this.primaryItem = null; } final Fragment fragment = (Fragment) object; final int itemIndex = fragments.indexOfValue(fragment); if (itemIndex >= 0) { final long itemId = fragments.keyAt(itemIndex); PagerAdaptersLogging.d(TAG, "Removing item(id: " + itemId + ", fragment: " + fragment + ", view: " + fragment.getView() + ")."); if (fragment.isAdded()) { this.savedStates.put(itemId, fragmentManager.saveFragmentInstanceState(fragment)); } else { this.savedStates.remove(itemId); } this.fragments.remove(itemId); } this.pendingTransaction.remove(fragment); } }
FragmentStatePagerAdapter extends PagerAdapter { @SuppressLint("CommitTransaction") @Override public void destroyItem(@NonNull final ViewGroup container, final int position, @NonNull final Object object) { if (pendingTransaction == null) { this.pendingTransaction = fragmentManager.beginTransaction(); } if (position == primaryPosition) { this.primaryPosition = NO_POSITION; this.primaryItem = null; } final Fragment fragment = (Fragment) object; final int itemIndex = fragments.indexOfValue(fragment); if (itemIndex >= 0) { final long itemId = fragments.keyAt(itemIndex); PagerAdaptersLogging.d(TAG, "Removing item(id: " + itemId + ", fragment: " + fragment + ", view: " + fragment.getView() + ")."); if (fragment.isAdded()) { this.savedStates.put(itemId, fragmentManager.saveFragmentInstanceState(fragment)); } else { this.savedStates.remove(itemId); } this.fragments.remove(itemId); } this.pendingTransaction.remove(fragment); } FragmentStatePagerAdapter(@NonNull final FragmentManager fragmentManager); }
FragmentStatePagerAdapter extends PagerAdapter { @SuppressLint("CommitTransaction") @Override public void destroyItem(@NonNull final ViewGroup container, final int position, @NonNull final Object object) { if (pendingTransaction == null) { this.pendingTransaction = fragmentManager.beginTransaction(); } if (position == primaryPosition) { this.primaryPosition = NO_POSITION; this.primaryItem = null; } final Fragment fragment = (Fragment) object; final int itemIndex = fragments.indexOfValue(fragment); if (itemIndex >= 0) { final long itemId = fragments.keyAt(itemIndex); PagerAdaptersLogging.d(TAG, "Removing item(id: " + itemId + ", fragment: " + fragment + ", view: " + fragment.getView() + ")."); if (fragment.isAdded()) { this.savedStates.put(itemId, fragmentManager.saveFragmentInstanceState(fragment)); } else { this.savedStates.remove(itemId); } this.fragments.remove(itemId); } this.pendingTransaction.remove(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 { @SuppressLint("CommitTransaction") @Override public void destroyItem(@NonNull final ViewGroup container, final int position, @NonNull final Object object) { if (pendingTransaction == null) { this.pendingTransaction = fragmentManager.beginTransaction(); } if (position == primaryPosition) { this.primaryPosition = NO_POSITION; this.primaryItem = null; } final Fragment fragment = (Fragment) object; final int itemIndex = fragments.indexOfValue(fragment); if (itemIndex >= 0) { final long itemId = fragments.keyAt(itemIndex); PagerAdaptersLogging.d(TAG, "Removing item(id: " + itemId + ", fragment: " + fragment + ", view: " + fragment.getView() + ")."); if (fragment.isAdded()) { this.savedStates.put(itemId, fragmentManager.saveFragmentInstanceState(fragment)); } else { this.savedStates.remove(itemId); } this.fragments.remove(itemId); } this.pendingTransaction.remove(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; }