android - OutOfMemory error when sliding through multiple images in ViewPager -


i using viewpager object within activity. viewpager works gallery shows multiple types of fragment (textfragment shows text, imagefragment, videofragment, etc.).

i using fragmentstatepageradapter i've gathered (and saw myself) frees previous elements of viewpager navigate through them.

if slide through gallery works fine, watching logcat shows memory being allocated , deallocated (at least, seems proper me). if slide fast through gallery, seems though vm not capable of freeing memory fast enough , end getting outofmemory exception.

is there way prevent this?

this initialize activity.

    @override     public void oncreate(bundle savedinstancestate) {  madapter = new myadapter(getsupportfragmentmanager()); mpager = (viewpager) findviewbyid(r.id.pager);         mpager.setcurrentitem(currentitem);         mpager.setadapter(madapter);         mpager.setoffscreenpagelimit(0);          circlepageindicator indicator = (circlepageindicator) findviewbyid(r.id.indicator);         mindicator = indicator;         indicator.setviewpager(mpager);  } 

and adapter:

public class myadapter extends fragmentstatepageradapter {          public myadapter(fragmentmanager fm) {             super(fm);         }          @override         public int getcount() {             return multimedialist.size();         }          @override         public fragment getitem(int position) {             multimedia m = multimedialist.get(position);             if (m.gettipo() == mmtype.imagen) {                 imagefragment ifragment = new imagefragment();                 bundle bundle = new bundle();                 bundle.putint("id", m.getid());                 ifragment.setarguments(bundle);                 return ifragment;             }             else if (m.gettipo() == mmtype.video) {                 videofragment ifragment = new videofragment();                 bundle bundle = new bundle();                 bundle.putint("id", m.getid());                 ifragment.setarguments(bundle);                 return ifragment;             }              else if (m.gettipo() == mmtype.labeledimage) {                 labeledimagefragment ifragment = new labeledimagefragment();                 bundle bundle = new bundle();                 bundle.putint("id", m.getid());                 ifragment.setarguments(bundle);                 return ifragment;              }             else if (m.gettipo() == mmtype.texto) {                 textfragment ifragment = new textfragment();                 bundle bundle = new bundle();                 bundle.putint("id", m.getid());                 ifragment.setarguments(bundle);                 return ifragment;              }              return null;         }      } 


Comments