android - How transaction to backstack works -


in app, have navigation drawer 3 items. when click on item, loads , replace actual fragment. fragment should save stack, when click back, should placed on screen saved state. also, when click on in navigation drawer, same stack saved state should appear.

i have tried call onbackpressed since should need, load saved state of fragment stack. when press or click on in navdrawer, nothing happens.

my code

public void changefragment(int position) {     fragtransaction = fragmanager.begintransaction();      switch(position) {         // main fragment; previous state should in stack         case 0:             appbar.settitle(leftmenutitles[position]);             onbackpressed();             break;         // doesn't matter if state lost         case 1:             appbar.settitle(leftmenutitles[position]);             fragtransaction.replace(r.id.fragment_holder, fragmentdatabase)                     .settransition(fragmenttransaction.transit_fragment_open)                     .commit();             break;         // doesn't matter if state lost         case 2:             appbar.settitle(leftmenutitles[position]);             fragtransaction.replace(r.id.fragment_holder, fragmentsettings)                     .settransition(fragmenttransaction.transit_fragment_open)                     .commit();             break;         // when no fragment found, make me delicious toast         default:             toast.maketext(this, "no such position in navdraw " + position, toast.length_long).show();     }        // close navigationdrawer      drawerlayout.closedrawer(leftlinearlayoutnavdrawer); } 

so thankful if explaind me. understand have read, when call addtobackstack(), saves transaction, not fargment. , can't find how load fragment stack without calling onbackpressed(). have read android developer site , many questions here, still can't figure out how fragment transaction works.

you’re correct in assumption, addtobackstack() saves whole transaction – is, saves operation did on fragmenttransaction undoing. means, if replace() in transaction, previous fragment restored when pop stack.

to go in stack, call get(support)fragmentmanager().popbackstackimmediate();, undoes changes of last transaction added stack.

in case, should not call onbackpressed() callback yourself, called android system.

i’ll try explain via code. in oncreate(), add first fragment:

fragmanager.begintransaction();     .replace(r.id.fragment_holder, fragmentmain)     .commit(); 

then, when navigation drawer menu items pressed, replace fragment , add stack (the replacing undone when pressed).

public void changefragment(int position) {     fragtransaction = fragmanager.begintransaction();      switch(position) {         // main fragment; previous state should in stack         case 0:             appbar.settitle(leftmenutitles[position]);             fragtransaction.replace(r.id.fragment_holder, fragmentmain)                     .settransition(fragmenttransaction.transit_fragment_open)                     .addtobackstack(null)                     .commit();             break;         // doesn't matter if state lost         case 1:             appbar.settitle(leftmenutitles[position]);             fragtransaction.replace(r.id.fragment_holder, fragmentdatabase)                     .settransition(fragmenttransaction.transit_fragment_open)                     .addtobackstack(null)                     .commit();             break;         // doesn't matter if state lost         case 2:             appbar.settitle(leftmenutitles[position]);             fragtransaction.replace(r.id.fragment_holder, fragmentsettings)                     .settransition(fragmenttransaction.transit_fragment_open)                     .addtobackstack(null)                     .commit();             break;         // when no fragment found, make me delicious toast         default:             toast.maketext(this, "no such position in navdraw " + position, toast.length_long).show();     }      // close navigationdrawer      drawerlayout.closedrawer(leftlinearlayoutnavdrawer); } 

however, i’m not sure if want. it’s best practice not use stack fragment transactions initiated navigation drawer menu item selection.


Comments