I switched my Activity to a AppCompatActivity and now calling invalidateOptionsMenu() in onOptionsItemSelected() no longer updates the Menu items as they did before. onPrepareOptionsMenu() is not called.
I added the following dependency
compile "com.android.support:appcompat-v7:22.2.1"
And updated my Activity to a AppCompatActivity (note only the first call to invalidateOptionsMenu() in onResume() works, the the other two do not):
public class MyActivity extends AppCompatActivity {
private boolean isStopSaved;
// ...
@Override
protected void onResume() {
super.onResume();
if (/* Check DB if star should be set */) {
isStopSaved = true;
} else {
isStopSaved = false;
}
invalidateOptionsMenu(); // This updates the menu as expected
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// ...
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_schedule, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_starred_add:
isStopSaved = true;
invalidateOptionsMenu(); // Does not update menu
break;
case R.id.menu_starred_remove:
isStopSaved = false;
invalidateOptionsMenu(); // Does not update menu
break;
}
return super.onOptionsItemSelected(item);
}
Edit: My device is running Android M Preview 2.
You should call supportInvalidateOptionsMenu(), which is meant to work with the support libraries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With