Search code examples
androidandroid-activityandroid-fragmentsandroid-fragmentactivityandroid-actionbar-compat

ActionBar Badge returns optionitemselected click to Activity instead of my Fragment


I have onOptionsItemSelected(MenuItem item) in my fragment. Now I'm forced to use Android-ActionItemBadge library(https://github.com/mikepenz/Android-ActionItemBadge), to add the ActionBar Notification Count. so I added the piece of code in my Fragment.

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            //Inflating the Menu
                inflater.inflate(R.menu.refresh_menu, menu);

        //inflating Notification Icon
        if (badgeCount > 0) {
            ActionItemBadge.update(getActivity(), menu.findItem(R.id.badge),
                    FontAwesome.Icon.faw_android, ActionItemBadge.BadgeStyle.DARKGREY, badgeCount);
        } else {
            ActionItemBadge.hide(menu.findItem(R.id.badge));
        }
}

But this Optionsitemselected return the value to my Activity but not in to my Fragment. any Idea? I want to Handle this Optionsitemselected in my Fragment.


Solution

  • In your fragment you need to call:

    setHasOptionsMenu(true);
    

    Edit:

    Since this custom ActionBar item isn't providing the calls to your fragment you can simply do it manually:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search: // Your item id
                Fragment f = getFragmentManager().findFragmentById(R.id.fragment_container);
                f.onOptionsItemSelected(item);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }