java - Make the extra action bar icon go away -


i've managed work around issue in icon appears in action bar after fragment transaction, clumsy solution , wonder if there better way solve it.

i have fragment class hierarchy in contentfragment abstract fragment occupies whole screen (except action bar) , subclasses contribute additional action bar icons (by means of respective oncreateoptionsmenu() , onoptionsitemselected() methods). e.g. contentfragmenta contributes icon a, contentfragmentb icon b, contentfragmentb may or may not child class of contentfragmenta (if is, action bar contain both icon a , icon b side side), , on.

initially (after user has logged in) screen contains contentfragmenta , action bar has icon a. user navigates through app other content fragments (or more precisely fragment transactions) added stack , icons correspondingly added or removed action bar.

it behaves nicely until user decides log out prompts app clear whole stack (bringing contentfragmenta after oldest transaction rolled back) , add logincontentfragment, contributes new profile icon action bar. @ moment icon a being shown beside new profile icon , don't want shown; issue i'm facing. should go away when user logs out.

i solved issue clearing stack usual , including transaction replaces contentfragmenta blank, icon-less content fragment sethasoptionsmenu(false), icon a gone when blank fragment replaced login fragment. find clumsy , think there might better way.

i have tried out calling menu.clear() in contentfragment superclass , activity.supportinvalidateoptionsmenu() in fragment replacement step neither seems work. menu.clear() in particular make icons go away leaving none in action bar.

does know alternative?

relevant code:

contentfragment.java:

public abstract class contentfragment extends fragment {      public static interface callbacks {         public abstract void setcurrentcontentfragment(contentfragment contentfragment);     }      protected callbacks mcallbacks;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         sethasoptionsmenu(true);     }      @override     public void onstart() {         super.onstart();         mcallbacks = (callbacks)getactivity();         mcallbacks.setcurrentcontentfragment(this);     }      @override     public void onstop() {         super.onstop();         mcallbacks = null;     }      public boolean handlebackpressed() {         return false;     } } 

contentfragmenta.java:

public abstract class contentfragmenta extends contentfragment {      protected abstract void handleiconatouched();      ...      @override     public void oncreateoptionsmenu(menu menu, menuinflater inflater) {         if (menu.finditem(r.id.icon_a) == null) {             inflater.inflate(r.menu.icon_a, menu);         }         super.oncreateoptionsmenu(menu, inflater);     }      @override     public boolean onoptionsitemselected(menuitem item) {         if (item.getitemid() == r.id.icon_a) {             handleiconatouched();             return true;         } else {             return super.onoptionsitemselected(item);         }     } } 

blankfragment.java:

public class blankfragment extends loggedincontentfragment {      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         sethasoptionsmenu(false);     } } 

in mainactivity.java:

...  private void addcontentfragmentnotaddingtransactiontobackstackifcurrentfragment(contentfragment fragment, boolean clearbackstack) {      // mcurrentcontentfragment changes stack cleared, addtobackstack calculated before clearbackstack() step.     boolean addtobackstack = (false == clearbackstack && (mcurrentcontentfragment != null && fragment.getclass() != mcurrentcontentfragment.getclass()));      if (clearbackstack) {         clearbackstack();     }      fragmenttransaction ft = getsupportfragmentmanager().begintransaction();     if (addtobackstack) {         ft.addtobackstack(null);     }     ft.replace(r.id.container, fragment);      ft.commit();      getsupportfragmentmanager().executependingtransactions(); }  public void clearbackstack() {     while (getsupportfragmentmanager().getbackstackentrycount() > 0) {         getsupportfragmentmanager().popbackstackimmediate();     }      // step avoid     fragmenttransaction ft = getsupportfragmentmanager().begintransaction();     ft.replace(r.id.container, new blankfragment());     ft.commit();      getsupportfragmentmanager().executependingtransactions(); }  ... 

i solved issue. problem fragment transaction stack. mixing transactions added stack ones weren't, , these causing stack popping work in unexpected way , prevent fragments being correctly removed adding icons action bar. issue explained in this question. solution adopted this one.


Comments