android - Preserving fragment state -


in app, have following architecture:

mainactivity |_ firstfragment |   |_ gridfragment |   |_ mapfragment | |_ secondfragment | |_ thirdfragment 

please note that:

  • mainactivity using actionbarsherlock , slidingmenu.
  • slidingmenu switches between fragments (firstfragment, secondfragment, thirdfragment).
  • firstfragment creates button action bar switch between grid , map mode. is, switch between subfragments.

what want achieve

i need fragments need preserve state when switching between them. is, if in firstfragment , select map mode (show mapfragment subfragment), , slide menu choose option , come back, i should see map again. right now, it's resetting each fragment when selecting section slide menu.

this isn't surprising, @ moment commiting transactions new firstfragment() when menu options pressed.

however, first tried have references each fragment parent. example, mainactivity had 3 fragments members which, when commiting transactions, checked if exist , instantiated if necessary. worked fine until added second level of fragments, started throwing exceptions when committing transaction (saying activity destroyed).

as may have noticed, far being android expert , need guidance on topic.

how can preserve fragment states without having reference each of them?

next thing tried after posting question, avoid keeping fragments in local variables inside container , using fragmentmanager.getfragmentbytag() access them @ given time, problem persisted, fragmenttransaction.replace() destroying fragments.

my solution

as luksprog pointed out in comment, had manage fragments manually. in order achieve this, had go former approach, had local variables each fragment. then, mainactivity following:

  • instantiate 3 fragments.

    this.firstfragment = new firstfragment(); this.secondfragment = new secondfragment(); this.thirdfragment = new thirdfragment(); 
  • attach 3 fragments, , hide of them except initial section.

    getsupportfragmentmanager()     .begintransaction()     .add(r.id.content_frame, this.firstfragment)     .add(r.id.content_frame, this.secondfragment)     .add(r.id.content_frame, this.thirdfragment)     .hide(this.secondfragment)     .hide(this.thirdfragment)     .commit(); 
  • to switch content, slidemenu calling function:

    public void switchcontent(fragment newcontent) {     if (newcontent != null) {         getsupportfragmentmanager()         .begintransaction()         .detach(this.firstfragment)         .detach(this.secondfragment)         .detach(this.thirdfragment)         .attach(newcontent)         .commit();          // restore menu open gesture if map not present         if (!(newcontent instanceof firstfragment) &&  getslidingmenu().gettouchmodeabove() != slidingmenu.touchmode_fullscreen)             getslidingmenu().settouchmodeabove(slidingmenu.touchmode_fullscreen);          // set menu open gesture if map present         if (newcontent instanceof firstfragment && firstfragment.currentfragment == firstfragment.map_fragment)             getslidingmenu().settouchmodeabove(slidingmenu.touchmode_none);          getslidingmenu().showcontent();     } } 

then, fragments has subfragments (firstfragment) doing same but:

  • it's using getchildfragmentmanager() instead of getsupportfragmentmanager().
  • it's using show() , hide() replace content, because when using detach() , attach() map wasn't preserving it's coordinates.

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -