Monday, 15 April 2013

android - Menu is null when activity gets recreated -



android - Menu is null when activity gets recreated -

i have inflated action bar menu in base of operations activity , modifying action bar items in base of operations activity. when alter orientation, menu null , thats why doesn't update action bar items. why null? here tried code:

@override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.base, menu); mmenu = menu; homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { // switch (item.getitemid()) { if (item.getitemid() == r.id.action_refresh) { refreshnewsdata(); homecoming true; } if (item.getitemid() == r.id.action_share) { sharenewsdata(); homecoming true; } if (item.getitemid() == r.id.action_favorite) { addfavorites(); homecoming true; } homecoming super.onoptionsitemselected(item); }

i have written code in base of operations activity. protected void showicons(int id) {

if (mmenu != null) { menuitem item = mmenu.finditem(id); item.setvisible(true); } } protected void hideicons(int id) { if (mmenu != null) { menuitem item = mmenu.finditem(id); item.setvisible(false); }

i'm using these methods update action bar menu items

you cannot update actionbar menu items directly. whenever want update menu items, create phone call invalidateoptionsmenu() this'll redraw action bar menus , create phone call onprepareoptionsmenu(). you'll have take care of menu items want show / hide within onprepareoptionsmenu().

android android-actionbar

No comments:

Post a Comment