android - OnOptionsItemSelected in activity is called before onOptionsItemSelected in fragment. Other way possible? -


i have activity can contain several fragments. each of fragments can have own menu entries in actionbar. works fine far , each item clickable , performs desired action.

my problem following. in mainactivity declared following lines intercept calls homeicon of actionbar:

public boolean onoptionsitemselected(menuitem item) {         switch (item.getitemid()) {         case android.r.id.home:             clearbackstack();                     sethomefragment();             return true;         default:             return super.onoptionsitemselected(item);          }     } 

i declared in activity because wanted every fragment should call don't have catch android.r.id.home case in each fragment.

in 1 fragment using setdisplayhomeasupenabled(true), little arrow left of actionbar icon. when homeicon clicked in fragment don't want set homefragment, want set fragment last displayed. have onoptionsitemselected - method in fragment:

@override public boolean onoptionsitemselected(menuitem menuitem) {      switch (menuitem.getitemid()) {     case android.r.id.home:         setlastfragment();                return true;     ... 

however not work way wanted work. activity's onoptionsitemselected called first, catches menuitem , redirects homefragment. other menuitems declared in other fragments can check see same behaviour. activity called first, doesn't catch menuitem (default case) , redirects super.onoptionsitemselected(item).

so seems case how android handles menu clicks. first activity, fragment. there way change this? don't want put android.r.id.home-case in every fragment , handle there. there nicer way this?

according developers reference,

"return false allow normal menu processing proceed, true consume here."

so try returning 'false' default in activity's implementation of onoptionsitemselected(), way event pass on fragment's implementation if not caught.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -