I would suggest keeping a reference within the activity to the Menu object you receive in `onCreateOptionsMenu` and then using that to retrieve the `MenuItem` that requires the change as and when you need it. For example, you could do something along the lines of the following:
public class YourActivity extends Activity {
private Menu menu;
private String inMenuTitle = "Set to In";
private String outMenuTitle = "Set to Out";
private boolean flag = false;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Create your menu...
this.menu = menu;
return true;
}
private void updateMenuTitles() {
MenuItem menuItem = menu.findItem(R.id.bedSwitch);
if (flag) {
menuItem .setTitle(outMenuTitle);
} else {
menuItem .setTitle(inMenuTitle);
}
}
}
Alternatively, you can override `onPrepareOptionsMenu` to update the menu items each time the menu is displayed.
I would suggest keeping a reference within the activity to the Menu object you receive in [onCreateOptionsMenu][1] and then using that to retrieve the MenuItem that requires the change as and when you need it. For example, you could do something along the lines of the following:
public class YourActivity extends Activity {
private Menu menu;
private String inBedMenuTitle = "Set to 'In bed'";
private String outOfBedMenuTitle = "Set to 'Out of bed'";
private boolean inBed = false;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Create your menu...
this.menu = menu;
return true;
}
private void updateMenuTitles() {
MenuItem bedMenuItem = menu.findItem(R.id.bedSwitch);
if (inBed) {
bedMenuItem.setTitle(outOfBedMenuTitle);
} else {
bedMenuItem.setTitle(inBedMenuTitle);
}
}
}
Alternatively, you can override [onPrepareOptionsMenu][2] to update the menu items each time the menu is displayed.
[1]: http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu%28android.view.Menu%29 "onCreateOptionsMenu"
[2]: http://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu%28android.view.Menu%29 "onPrepareOptionsMenu"