onMenuItemClick doesn't get called

1.5k views Asked by At

I am using NavigationDrawer with ActionBarSherlock. When I click on menu items onMenuItemClick() methods don't get called.

The code:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == android.R.id.home) {

        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {

            mDrawerLayout.closeDrawer(mDrawerList);
        } else {

            mDrawerLayout.openDrawer(mDrawerList);
        }
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    return super.onPrepareOptionsMenu(menu);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {

    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {

    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    setTitle(AndroidUtils.getTitle());
    SubMenu subMenu1 = menu.addSubMenu(null);
    subMenu1.add(getString(R.string.save_loc)).setIcon(android.R.drawable.ic_menu_save).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {

            ...
        } 
    });
    subMenu1.add(getString(R.string.save_em)).setIcon(android.R.drawable.ic_dialog_email).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {

            ...
        }
    });
    MenuItem subMenu1Item = subMenu1.getItem();
    subMenu1Item.setIcon(android.R.drawable.ic_menu_share);
    subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    return super.onCreateOptionsMenu(menu);
}
2

There are 2 answers

0
Bruce On

Since you are already using onOptionsItemSelected, have you considered processing all of your menu items in there instead? Like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    setTitle(AndroidUtils.getTitle());

    SubMenu subMenu1 = menu.addSubMenu(null);

    subMenu1.add(0, R.id.menu_save_loc, 0, R.string.save_loc)
        .setIcon(android.R.drawable.ic_menu_save);
    subMenu1.add(0, R.id.menu_save_em, 0, R.string.save_em)
        .setIcon(android.R.drawable.ic_dialog_email);

    MenuItem subMenu1Item = subMenu1.getItem();
    subMenu1Item.setIcon(android.R.drawable.ic_menu_share);
    subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
            | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();
    if (itemId == android.R.id.home) {
        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            mDrawerLayout.openDrawer(mDrawerList);
        }
    }
    else if (itemId == R.id.menu_save_loc) {
        ...
    }
    else if (itemId == R.id.menu_save_em) {
        ...
    }
    else {
        return super.onOptionsItemSelected(item);
    }

    return true;
}
0
Tomin On

For some strange reasons, I was not able to handle menu item clicks, but setting the setOnClickListener for each menu view helped.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.media_process_menu, menu);
    for (int i = 0; i < menu.size(); i++) {
        menu.getItem(i).getActionView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.save) {
                    //Your action
                    handleSaveAction();
                }
            }
        });
    }

    return super.onCreateOptionsMenu(menu);
}