I have some memory leak problem I can't find the way to solve. There are many questions in stackoverflow but no one of them was useful for me.
I have an activitiy with a view pager and action bar tabs navigation. In every fragment I load only one image so that this is sort of a gallery in the end. That works nice.
After some fragment changes, I got this error:
E/AndroidRuntime(19271): java.lang.OutOfMemoryError E/AndroidRuntime(19271): at android.graphics.Bitmap.nativeCreate(Native Method)
All I've read is about the need of deallocating images, so I thought something about:
- Event at "fragment lost focus"
- Delete all views inside the fragment whose focus has lost
But I can't find any solution like that. Is that the correct way? If so, can you give an example? Or I'm completely wrong and the solution is another way?
This is how I add the images to the fragment:
ImageView img = new ImageView(rootView.getContext());
img.setVisibility(1);
img.setImageResource(R.drawable.gallery_image_1);
img.setAdjustViewBounds(true);
img.setTag( "gallery_image_1" );
gallery.addView(img);
Thanks to all in advance!
Diego