android - How to add Menu option in Activity from TabHost -


i have tabhost inside there 5 activities. problem if adding menu option on actionbar each activity, not displaying on actionbar.

if add menu option tabhost activity, getting displayed on actionbar.

but want add different action each activity when menu option clicked.

please me how can achieve ??

the easiest way use tabhost ontabchangedlistener.

    tabhost.setontabchangedlistener(new tabhost.ontabchangelistener() {         @override         public void ontabchanged(string tabid) {             switch (tabid) {                 case "tab1":                     //do stuff                     break;                   ...etc                 }         }     }); 

this keep track of tab clicked, , can manually swap action there.

edit expand on little more: change action (or onclicklistener if you're using custom action bar) menu button here. appearances, nothing changes upon each tab click, action menuoptions perform. messy code needs optimized; you'll have yourself:

tabhost.setontabchangedlistener(new tabhost.ontabchangelistener() {     @override     public void ontabchanged(string tabid) {         switch (tabid) {             case "tab1":                 activity = "tab1";                 break;             case "tab2":                 activity = "tab2";                 break;             //etc         }     } }); 

and lower in parent tabhost activity, have respond menu button presses:

@override public boolean onoptionsitemselected(menuitem item) {     switch (item.getitemid()) {         //this handles actions custom button         case r.id.menu_options:             //we further customize here listening value of "activity" , performing actions based on             switch (activity) {                 case "tab1":                     //place actual menu action here                     break;                 case "tab2":                     break;                 //etc             }             return true;         default:             return super.onoptionsitemselected(item);     } } 

edit2: browsing on stackoverflow gives can have different menu each tab of tabhost, similar not quite same since want different menus, not same menu different actions. might worth look, though.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -