Search code examples
androidactionbarsherlockandroid-actionbar

How to refresh Action Bar menu in fragments


I am using the onCreateOptionsMenu to display the action bar Sherlock menu in fragment. I have a requirement such that if a list is null then make menu to be disable and if present then enable and some options in menu to be changed. This is my code

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if ((list != null && list.size() > 0)
            || (response != null && response.messageInfo != null && response.messageInfo
                    .size() > 0)) {
        SubMenu submenus = menu.addSubMenu("");
        submenus.add(1, 1, 1, "Send New Message");
        submenus.add(1, 2, 2, "Filter by Category");
        MenuItem subitem = submenus.getItem();
        subitem.setIcon(R.drawable.img_sorting);
        subitem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    } else {
        menu.add(1, 10, 10, "NOT Clickable").setIcon(R.drawable.img_hide)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    }
}

So here when it loads for the first time it is in disable even when the list is there its still in disable mode until if I refresh it is not changing its properties.


Solution

  • if you are using Sherlock try calling supportInvalidateOptionsMenu(); otherwise try with invalidateOptionsMenu(); for the native ActionBar