@Test public void testDestroyItem() { final FragmentManager mockFragmentManager = mock(FragmentManager.class); final FragmentPagerAdapter 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(mockFragmentManager).beginTransaction(); verify(mockTransaction).detach(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; } PagerAdaptersLogging.d(TAG, "Detaching item(position: " + position + ", id: " + getItemId(position) + ", fragment: " + object + ", view: " + ((Fragment) object).getView() + ")."); this.pendingTransaction.detach((Fragment) object); }
FragmentPagerAdapter 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; } PagerAdaptersLogging.d(TAG, "Detaching item(position: " + position + ", id: " + getItemId(position) + ", fragment: " + object + ", view: " + ((Fragment) object).getView() + ")."); this.pendingTransaction.detach((Fragment) object); } }
FragmentPagerAdapter 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; } PagerAdaptersLogging.d(TAG, "Detaching item(position: " + position + ", id: " + getItemId(position) + ", fragment: " + object + ", view: " + ((Fragment) object).getView() + ")."); this.pendingTransaction.detach((Fragment) object); } FragmentPagerAdapter(@NonNull final FragmentManager fragmentManager); }
FragmentPagerAdapter 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; } PagerAdaptersLogging.d(TAG, "Detaching item(position: " + position + ", id: " + getItemId(position) + ", fragment: " + object + ", view: " + ((Fragment) object).getView() + ")."); this.pendingTransaction.detach((Fragment) object); } FragmentPagerAdapter(@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); }
FragmentPagerAdapter 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; } PagerAdaptersLogging.d(TAG, "Detaching item(position: " + position + ", id: " + getItemId(position) + ", fragment: " + object + ", view: " + ((Fragment) object).getView() + ")."); this.pendingTransaction.detach((Fragment) object); } FragmentPagerAdapter(@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); static final int NO_POSITION; static final long NO_ID; }