Search code examples
androidactionbarsherlockmenuitem

Menu not refreshing on some devices


I have a problem with strange menu behavior on some devices. What i do, is showing/hiding menu items according to application state. I'm using Actionbarsherlock. My code is:

private MenuItem saveMI;
private MenuItem postMI;
private MenuItem editMI;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    saveMI = menu.findItem(R.id.menu_save);
    postMI = menu.findItem(R.id.menu_post);
    editMI = menu.findItem(R.id.menu_edit);
    if (app != null) {
        app.setCurrentState();
    }
    return true;
}
void setGlobalState(State state, Object menuItem, String title) {
    getSupportActionBar().setTitle(title);
    boolean showSaveMenu = true;
    boolean showPostMenu = true;
    boolean showEditMenu = true;
    switch (state) {
    case EDITING:
        showSaveMenu = false;
        showPostMenu = true;
        showEditMenu = false;
        break;
    case VIEWING:
        showSaveMenu = false;
        showPostMenu = true;
        showEditMenu = true;
        break;
    case EDITING_NEEDTOSAVE:
        showSaveMenu = true;
        showPostMenu = true;
        showEditMenu = false;
        break;
    case IMG_EDITING:
    case POSTING:
    case NOT_POST:
    case NEW_POST:
    case CHOOSING_ACCS:
        showSaveMenu = false;
        showPostMenu = false;
        showEditMenu = false;
        break;
    }
    if (saveMI != null) {
        saveMI.setVisible(showSaveMenu);
    }
    if (postMI != null) {
        postMI.setVisible(showPostMenu);
    }
    if (editMI != null) {
        editMI.setVisible(showEditMenu);
    }
    invalidateOptionsMenu();
    supportInvalidateOptionsMenu();
}

Actually, it works fine on pure Android. Tested on a lot amount of devices. But there is some devices, that suppose to ignoring all menu changes, and menu items remain invisible whatever state i set. For now, i saw that on two devices: Huawei S7-701u tablet, and Sony Xperia phone. I'm afraid, that vendors changed something with actionbar in firmware... So, can you help me to get a normal behavior on all devices?

EDITED I mentioned, that if i don't call hide menu items at application start, they appear in actionbar. But then i cannot hide them at all. So, it seems like actionbar on that devices doesn't receive invalidate call. Still stuck, need help.


Solution

  • If someone is interesting, i found solution by myself. I transferred all code from setGlobalState method to onCreateOptionsMenu, and removed class fields for menu items - an it worked.