Android memory leak issue when using ViewPagerAdapter with nested fragments -
i have fragment, fragment a, holds viewpager. viewpager loads different fragments user can swipe through "indefinitely" (i use high number of pages/loops emulate this). when user clicks on current viewpager fragment, fragment viewpager replaced fragment b in fragment manager. when user returns fragment b, backstack popped using popbackstackimmediate()
. if user repeats action several times, heap begins fill 100kb @ time until app starts become sloppy , malfunction memory fills up. i'm unsure causing this, can help?
my fragment viewpager:
public class mainfragment extends fragment { private mainwearactivity mmainwearactivity; view view; private int currentpage; private viewpager pager; private viewpageradapter adapter; private linearlayout helpicons; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mmainwearactivity = (mainwearactivity) getactivity(); adapter = new viewpageradapter(this.getchildfragmentmanager()); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view = inflater.inflate(r.layout.fragment_main, container, false); // scrolling menu pager = (viewpager) view.findviewbyid(r.id.watchnavpager); pager.setadapter(adapter); pager.addonpagechangelistener(adapter); // set current item middle page pager.setcurrentitem(consts.first_page); currentpage = consts.first_page; // set number of pages pager.setoffscreenpagelimit(4); // set no margin other pages hidden pager.setpagemargin(0); return view; } @override public void ondestroyview() { pager = null; super.ondestroyview(); } }
my adapter class:
public class viewpageradapter extends fragmentpageradapter implements viewpager.onpagechangelistener { public viewpageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { position = position % consts.pages; switch(position){ case consts.audio_pos: return new adapteraudiofragment(); case consts.voice_pos: return new adaptervoicefragment(); case consts.mail_pos: return new adaptermailfragment(); case consts.info_pos: return new adapterinfofragment(); default: return null; } } @override public int getcount() { return consts.pages * consts.loops; // (4 * 1000) } @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {} @override public void onpageselected(int position) {} @override public void onpagescrollstatechanged(int state) {} }
one of fragments adapter loads (they pretty same):
public class adapteraudiofragment extends fragment { private imageview menuimg; private textview menutext; private linearlayout rootview; private mainwearactivity mmainwearactivity; private view.onclicklistener imgclicklistener; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mmainwearactivity = (mainwearactivity) getactivity(); imgclicklistener = new view.onclicklistener() { @override public void onclick(view v) { mmainwearactivity.replacefragment(mmainwearactivity.getfragment(consts.frag_audio), consts.frag_audio); } }; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // root view of fragment layout rootview = (linearlayout) inflater.inflate( r.layout.fragment_nav_object, container, false); // set current menu image , text menuimg = (imageview) rootview.findviewbyid(r.id.fragment_image); menuimg.setimageresource(r.mipmap.ic_audio); menuimg.setonclicklistener(imgclicklistener); menutext = (textview) rootview.findviewbyid(r.id.menutxt); menutext.settext(consts.menu_header_audio); // set current menu selection mmainwearactivity.setcurrentselection(consts.audio_pos); return rootview; } }
i have feeling adapter's fragments being created never destroyed , piling in heap can't figure out how resolve this. need call destroyitem in adapter , manually destroy them? appreciated, thanks.
there specific pageradapter needs - fragmentstatepageradapter
this version of pager more useful when there large number of pages, working more list view. when pages not visible user, entire fragment may destroyed, keeping saved state of fragment. allows pager hold on less memory associated each visited page compared fragmentpageradapter @ cost of potentially more overhead when switching between pages.
Comments
Post a Comment