Mixing menuItem.setIntent with onOptionsItemSelected doesn't work

Posted by superjos on Stack Overflow See other posts from Stack Overflow or by superjos
Published on 2010-12-30T21:50:48Z Indexed on 2010/12/30 21:53 UTC
Read the original article Hit count: 201

Filed under:
|
|
|

While extending a sample Android activity that fires some other activities from its menu, I came to have some menu items handled within onOptionsItemSelected, and some menu items (that just fired intents) handled by calling setIntent within onCreateOptionsMenu.

Basically something like:

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    super.onCreateOptionsMenu(menu);

    menu.add(0, MENU_ID_1, Menu.NONE, R.string.menu_text_1);
    menu.add(0, MENU_ID_2, Menu.NONE, R.string.menu_text_2);

    menu.add(0, MENU_ID_3, Menu.NONE, R.string.menu_text_3).
        setIntent(new Intent(this, MyActivity_3.class));

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    super.onOptionsItemSelected(item);

    switch (item.getItemId())
    {
        case (MENU_ID_1):
            // Process menu command 1 ...
            return true;

        case (MENU_ID_2):
            // Process menu command 2 ...
            // E.g. also fire Intent for MyActivity_2 
            return true;

        default:
            return false;
    }
}

Apparently, in this situation the Intent set on MENU_ID_3 is never fired, or anyway the related activity is never started.

Android javadoc at some point goes like <<[if you set an intent on a menu item] and nothing else handles the item, then the default behavior will be to [start the activity with the intent]>>.

What does it actually mean "and nothing else handles the item"? Is it enough to return false from onOptionsItemSelected? I also tried not to call super.onOptionsItemSelected(item) at the beginning and only invoke it in the default switch case, but I had same results.

Does anyone have any suggestion? Does Android allow to mix the two type of handling?

Thanks for your time everyone.

© Stack Overflow or respective owner

Related posts about android

Related posts about activity