android - Programatically added Fragment to FrameLayout is not shown after orientation change -


  1. i have activity holds 1 fragment.
  2. in fragment (that has relativelayout set content view) add programmatically many framelayouts.
  3. during adding set id framelayouts using view.generateid().
  4. then add programmatically nested fragments framelayouts (let's call them slots).
  5. finally add again nested fragments time slots. , in slots can add myimagefragment, myvideofragment, mywebviewfragment.
  6. using scheduledthreadpoolexecutor change content of slots

the problem encounter when rotate device not see fragments add.

i reuse fragments using findfragmentbytag().

hierarchy before rotation:

before rotation

and after rotation:

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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -