android - FragmentStatePageAdapter doesn't remove child fragments -
having problem fragmentstatepageradapter
, screen change orientation (saving state guess). layout of app following:
activity --> fragment fragment b (contains fragmentstatepageradapter) wich shows 2 fragments --> fragment b1 fragment b2
all fragments mentioned inherit basefragment
class doesn't special keeping "global" vars need. when activity
starts loads fragment a
. menu replace fragment a
fragment b
. fragment b
source following:
public class fragment b extends basefragment { public static string tag = "b_fragment"; private mycustompageradapter adapter; private arraylist<string> tags = new arraylist<>(); private viewpager mviewpager; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); adapter = new mycustompageradapter(getfragmentmanager()); //adapter = new mycustompageradapter(getchildfragmentmanager()); tags.add(fragmentb1.tag); tags.add(fragmentb2.tag); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_sample, container, false); } @override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); mviewpager = (viewpager) view.findviewbyid(r.id.viewpager); mviewpager.setadapter(adapter); slidingtablayout mslidingtablayout = (slidingtablayout)view.findviewbyid(r.id.sliding_tabs); mslidingtablayout.setviewpager(mviewpager); } class mycustompageradapter extends fragmentstatepageradapter { //class mycustompageradapter extends fragmentpageradapter { fragmentmanager mgr; public sparsearray<fragment> registeredfragments = new sparsearray<fragment>(); public prepagatepageradapter(fragmentmanager mgr) { super(mgr); this.mgr = mgr; } @override public int getcount() { return 2; } @override public fragment getitem(int position) { switch (position) { case 0: return new fragmentb1(); case 1: return new fragmentb2(); } return null; } @override public object instantiateitem(viewgroup container, int position) { fragment fragment = (fragment) super.instantiateitem(container, position); registeredfragments.put(position, fragment); return fragment; } @override public void destroyitem(viewgroup container, int position, object object) { registeredfragments.remove(position); super.destroyitem(container, position, object); } public fragment getregisteredfragment(int position) { return registeredfragments.get(position); } } }
the problem happens when go fragment b
go again fragment a
, change orientation: see while debugging on onattach()
method of fragment a
in fragmentmanager
fragment b
no more present (replaced fragment a
) fragment b1
, b2
there , called rispective onattach()
methods, resulting in weird behaviours of layout. didn't child fragments should remove fragmentmanager
?
at moment don't know if more details needed. can give further if need.
Comments
Post a Comment