android - Nested fragments created twice on orientation change -
i created example understand lifecycle of android fragments @ screen orientation change.
mainactivity container drawerlayout, drawerlayout allows choose fragment, fill mainactivity screen.
public class mainactivity extends actionbaractivity { ... @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.d("mainactivity", "oncreate savedinstancestate = "+(savedinstancestate == null ? "null" : "not null")); ... mdrawerlist.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { if (position == 0) { if (viewingposition == position) { mdrawerlayout.closedrawer(mdrawerlist); return; } getsupportfragmentmanager() .begintransaction() .replace(r.id.contentframe, new parentpagerfragment(), parentpagerfragment.tag).commit(); viewingposition = 0; } if (position == 1) { if (viewingposition == position) { mdrawerlayout.closedrawer(mdrawerlist); return; } getsupportfragmentmanager().begintransaction() .replace(r.id.contentframe, childtextviewfragment.newinstance("hello fragment"), childtextviewfragment.tag) .commit(); viewingposition = 1; } } ... getsupportfragmentmanager() .begintransaction() .replace(r.id.contentframe, new parentpagerfragment(), parentpagerfragment.tag).commit(); ...
then have parentpagerfragment, parentpagerfragment contains viewpager 3 childtextviewfragments.
public class parentpagerfragment extends fragment { ... @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.d(tag, "oncreateview savedinstancestate "+(savedinstancestate == null ? "null" : "not null")); view v = inflater.inflate(r.layout.fragment_pager, container, false); list<childtextviewfragment> viewfragments = new arraylist<>(); viewfragments.add(childtextviewfragment.newinstance("fragment1")); viewfragments.add(childtextviewfragment.newinstance("fragment2")); viewfragments.add(childtextviewfragment.newinstance("fragment3")); mypageradapter mpageradapter = new mypageradapter(getchildfragmentmanager(), viewfragments); viewpager mviewpager = (viewpager) v.findviewbyid(r.id.pager); mviewpager.setadapter(mpageradapter); return v; } ... } class mypageradapter extends fragmentpageradapter { list<childtextviewfragment> viewfragments; public mypageradapter(fragmentmanager fm, list<childtextviewfragment> viewfragments) { super(fm); this.viewfragments = viewfragments; } @override public fragment getitem(int index) { return viewfragments.get(index); } @override public int getcount() { return viewfragments.size(); } @override public charsequence getpagetitle(int position){ if(position == 0) { return "fragment1"; } else if (position == 1) { return "fragment2"; } else { return "fragment3"; } } }
childtextviewfragment used display text
public class childtextviewfragment extends fragment { ... @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_text, container, false); textview mtextview = (textview) view.findviewbyid(r.id.textview1); string text = getarguments().getstring(text_key); mtextview.settext(text); log.d(tag, text+" :: oncreateview savedinstancestate " + (savedinstancestate == null ? "null" : "not null")); return view; }
after run example first time got these logs messages expected:
06-01 10:34:24.154: d/mainactivity(8426): oncreate savedinstancestate = null 06-01 10:34:24.272: d/parentpagerfragment(8426): oncreateview savedinstancestate null 06-01 10:34:24.389: d/childtextviewfragment(8426): fragment1 :: oncreateview savedinstancestate null 06-01 10:34:24.390: d/childtextviewfragment(8426): fragment2 :: oncreateview savedinstancestate null
the surprise appeared, when rotated display:
06-01 10:36:15.697: d/mainactivity(8426): oncreate savedinstancestate = not null 06-01 10:36:15.713: d/parentpagerfragment(8426): oncreateview savedinstancestate not null 06-01 10:36:15.716: d/childtextviewfragment(8426): fragment1 :: oncreateview savedinstancestate not null 06-01 10:36:15.717: d/childtextviewfragment(8426): fragment2 :: oncreateview savedinstancestate not null 06-01 10:36:15.718: d/parentpagerfragment(8426): oncreateview savedinstancestate null 06-01 10:36:15.739: d/childtextviewfragment(8426): fragment1 :: oncreateview savedinstancestate null 06-01 10:36:15.740: d/childtextviewfragment(8426): fragment2 :: oncreateview savedinstancestate null
i expecting fragments restored , displayed again (like first 4 line of logs shows), don't understand, why fragments created again savedinstancestate = null ? common behavior, or doing wrong ?
try adding null check savedinstancestate in mainactivity
oncreate(bundle savedinstancestate)
method this
if (savedinstancestate == null) { getsupportfragmentmanager() .begintransaction() .replace(r.id.contentframe, new parentpagerfragment(), parentpagerfragment.tag).commit(); }
Comments
Post a Comment