Tuesday, 13 August 2013

Changing the actionbar menu state depending on fragment

Changing the actionbar menu state depending on fragment

I am trying to show/hide items in my action bar depending on which
fragment is visible.
In my MainActivity I have the following
/* Called whenever invalidateOptionsMenu() is called */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(this.myFragment.isVisible()){
menu.findItem(R.id.action_read).setVisible(true);
}else{
menu.findItem(R.id.action_read).setVisible(false);
}
return super.onPrepareOptionsMenu(menu);
}
This works great however, when the device is rotated there is a issue.
After the rotation is complete onPrepareOptionsMenu is called again
however this time this.myFragment.isVisible() returns false...and hence
the menu item is hidden when clearly the fragment is visible (as far as
whats shown on the screen).

No comments:

Post a Comment