blueman3399 blueman3399 - 11 months ago 68
Android Question

PagerAdapter with only one Fragment that is instantiate

I would like to use a PagerAdapter but I want to instantiate only the showing Fragment.
In onDestroy I call this:

public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
Log.i("REMOVE", "destroyItem() [position: " + position + "]" + " childCount:" + container.getChildCount());

For example if I am at position 3 the fragment at position 2 and 4 is also instantiated. How can I destroy all Items except the showing?


You can't.

They are needed for the swipe animation.

If you only need one fragment to be instantiated at any given time, you should drop the ViewPager and use FragmentTransaction.replace() instead.