java - Fragment is not currently in the FragmentManager -
i searched on others similar posts didn't find solution problem.
i'm using library sliding menu (i know there exist "official solution" whatever) i've got fragmentchangeactivity :
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // set above view if (savedinstancestate != null) mcontent = getsupportfragmentmanager().getfragment(savedinstancestate, "mcontent"); if (mcontent == null) mcontent = new accueilfragment(); // set above view setcontentview(r.layout.content_frame); getsupportfragmentmanager() .begintransaction() .replace(r.id.content_frame, mcontent) .commit(); // set behind view setbehindcontentview(r.layout.menu_frame); getsupportfragmentmanager() .begintransaction() .replace(r.id.menu_frame, new applimenufragment()) .commit(); // customize slidingmenu getslidingmenu().settouchmodeabove(slidingmenu.touchmode_fullscreen); } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); getsupportfragmentmanager().putfragment(outstate, "mcontent", mcontent); } public void switchcontent(fragment fragment) { mcontent = fragment; string tag = null; getsupportfragmentmanager() .begintransaction() .replace(r.id.content_frame, fragment) .addtobackstack(tag) .commit(); getslidingmenu().showcontent(); }
from there, replace, on item click in menu, each fragment another. works fine. issue is, example:
fragment -> fragment b from fragment b click button i'm 1 time again on fragment , press home buttonhere i've got exception :
java.lang.illegalstateexception: fragment fragmenta not in fragmentmanager
i found problem there code below execute :
getsupportfragmentmanager().putfragment(outstate, "mcontent", mcontent);
but can't figure out why. help much apreciate !
----- edit -----
this happening when press button home button. in case juste press home button, getsupportfragmentmanager().putfragment, work juste fine.
i understand can't fragment transactions after savedinstancestate() called getsupportfragmentmanager().putfragment() isn't transaction right? or miss ? i'm not totally comfortable that.
----- solution -----
i still don't understand why getsupportfragmentmanager().putfragment() not possible there, removed , utilize findfragmentbytag in oncreate : if (savedinstancestate != null) mcontent = getsupportfragmentmanager().findfragmentbytag("customtag"); if (mcontent == null) mcontent = new accueilfragment(); have same behavior, maintain state when rotating , no more exception whe, press home.
i got here :issue getsupportfragmentmanager().putfragment
you cant create fragments transactions after onsaveinstancestate
java android android-fragments
No comments:
Post a Comment