2

I'm using a fragment with a ViewPager with a custom PagerAdapter to display bitmap images.

The bitmaps are loaded with the BitmapFactory on demand in the instantiateItem method and recycled in destroyItem. So while swiping left and right only 3 Bitmaps are in memory, and the old ones are destroyed.

When leaving the fragment, destroyItem is not called. Do I have to manually recycle the remaining items to avoid the bitmaps being leaked? Or are they automatically garbage collected when the ViewPager and the Adapter are destroyed?

Update: Because SilentKnight wants some code, here are the relevant parts of the adapter:

@Override
public Object instantiateItem(final ViewGroup container, final int position) {
    Log.wtf(LOGTAG, "Instantiate " + position);
    final ImageItem imageItem = new ImageItem();

    imageItem.imagePath = imagePaths.get(position);

    imageItem.bitmap = BitmapFactory.decodeFile(imageItem.imagePath);

    final View viewLayout = inflater.inflate(R.layout.media_image_item, container, false);

    final ImageView imageView = (ImageView) viewLayout.findViewById(R.id.imageView);
    imageView.setImageBitmap(imageItem.bitmap);

    imageItem.itemView = viewLayout;

    container.addView(viewLayout);

    return imageItem;
}

@Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
    Log.wtf(LOGTAG, "Destroy " + position);
    final ImageItem imageItem = (ImageItem) object;
    container.removeView(imageItem.itemView);
    imageItem.bitmap.recycle();
}

ImageItem is just simple holder:

private class ImageItem {
    private View itemView;
    private Bitmap bitmap;
    private String imagePath;
}
MatF
  • 1,728
  • 2
  • 14
  • 31

0 Answers0