android - Programatically added Fragment to FrameLayout is not shown after orientation change -
- i have
activity
holds 1fragment
. - in fragment (that has
relativelayout
set content view) add programmatically manyframelayouts
. - during adding set id framelayouts using
view.generateid()
. - then add programmatically nested fragments
framelayouts
(let's call them slots). - finally add again nested fragments time slots. , in slots can add
myimagefragment
,myvideofragment
,mywebviewfragment
. - using
scheduledthreadpoolexecutor
change content of slots
the problem encounter when rotate device not see fragments add.
i reuse fragments using findfragmentbytag()
.
hierarchy before rotation:
and after rotation:
how resolve problem? why fragments not attached?
the answer on lowest level (slot level) did not save value generated view.generateid()
. needed add part of code.
public static string layout_id = "layout_id"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (savedinstancestate != null) { framelayoutid = savedinstancestate.getint(layout_id); } else { framelayoutid = -1; } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_slot, container, false); if (framelayoutid == -1) framelayoutid = view.generateviewid() ; view.setid(framelayoutid); } @override public void onsaveinstancestate(bundle outstate) { outstate.putint(layout_id, framelayoutid); super.onsaveinstancestate(outstate); }
Comments
Post a Comment