Reputation: 23
I have a base activity. I'm inflating the action bar items from base activity. Now in my Main Activity, which extends Base Activity, I'm replacing the fragments.So while replacing I'm hiding refresh icon and showing share icon. But share icon gets displayed before the 2nd fragment is loaded. once 2nd fragment is displayed, again share icon is hide, Why so..Here is what i tried.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
mMenu = menu;
return super.onCreateOptionsMenu(menu);
}
private void showOption(int id) {
MenuItem item = mMenu.findItem(id);
item.setVisible(true);
}
private void hideOption(int id) {
MenuItem item = mMenu.findItem(id);
item.setVisible(false);
}
Methods for hiding and showing icons...
while replacing the fragment, I'm doing this :
replaceFragment(mDetailFragment);
showOption(R.id.action_share);
hideOption(R.id.action_refresh);
Upvotes: 0
Views: 1693
Reputation: 2258
You have to iterate through each menu item of your menu reference to change their property.
Menu mMenu ;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
mMenu = menu;
return super.onCreateOptionsMenu(menu);
}
private boolean changeVisibility(int menuId, boolean visibility) {
for (int i = 0; i < menu.size(); i++) {
if (mMenu .getItem(i).getItemId() == menuId) {
mMenu .getItem(i).setVisible(visibility);
}
}
}
Then use it any where in the activity as :
replaceFragment(mDetailFragment);
changeVisibility(R.id.action_share, true);
changeVisibility(R.id.action_refresh, false);
Upvotes: 2