I see the following misfeature on Android 4.0.4, HTC T-Mobile:
My program changes the options menu at run-time (namely, things like replacing a "Start" button with a "Stop" button). Unfortunately, the overflow menu disappears, and at least one menu item is "lost". On the first sight, it looks like the "More" menu item is replaced by another menu item, "Settings" in my case.
This does not happen on Android 2.
(I found a workaround and am posting this in case somebody else encounters this problem)
The workaround is not to let the number of visible items to decrease. It looks like Android 4 removes the "More" item when it's not needed but cannot add it when it is needed again.
I have changed my code from
private void doPrepareOptionsMenu(Menu menu) {
boolean running = ...;
menu.findItem(R.id.menu_stop).setVisible(running);
menu.findItem(R.id.menu_start).setVisible(!running);
}
to
private void doPrepareOptionsMenu(Menu menu) {
boolean running = ...;
if (running) {
menu.findItem(R.id.menu_stop).setVisible(running);
menu.findItem(R.id.menu_start).setVisible(!running);
} else {
menu.findItem(R.id.menu_start).setVisible(!running);
menu.findItem(R.id.menu_stop).setVisible(running);
}
}
and the problem disappeared. The difference is that we first make an item visible and only then make another item invisible.
In case you never changed the Android 2 menu at run-time, some context how it works:
SomeListener someListener = new SomeListener() {
public void someStateChanged() {
// This runs NOT on the UI thread
runOnUiThread(new Runnable() {
public void run() {
updateUI();
}
});
}
}
and updateUI()
finally calls the code
Menu menu = weakRefOptionsMenu.get();
if (null != menu) {
doPrepareOptionsMenu(menu);
}
and, of course, onPrepareOptionsMenu()
also calls doPrepareOptionsMenu()
:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
doPrepareOptionsMenu(menu);
return true;
}