将 menuItem.setIntent 与 onOptionsItemSelected 混合不起作用

发布于 2024-10-09 23:19:32 字数 1413 浏览 2 评论 0原文

在扩展一个从菜单中激发其他一些活动的示例 Android 活动时,我开始在 onOptionsItemSelected 中处理一些菜单项,并通过调用 setIntent 处理一些菜单项(刚刚激发意图) onCreateOptionsMenu 内。

基本上是这样的:

@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;
    }
}

显然,在这种情况下,在 MENU_ID_3 上设置的 Intent 永远不会被触发,或者无论如何,相关的活动永远不会启动。

Android javadoc 在某些时候会像<<[如果您在菜单项上设置意图]并且没有其他东西处理该项目,那么默认行为将是[使用该意图启动活动]>>。

“没有其他东西可以处理该项目”实际上是什么意思? 从 onOptionsItemSelected 返回 false 是否足够? 我还尝试在开始时不调用 super.onOptionsItemSelected(item) ,而仅在默认 switch 情况下调用它,但我得到了相同的结果。

有人有什么建议吗? Android 允许混合两种类型的处理吗?

感谢大家抽出时间。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居者 2024-10-16 23:19:32

好的。这个解决方案已经够愚蠢的了。清单中的目标活动名称(例如 MyActivity_3)在清单中拼写错误。

我将第三个菜单项处理更改为 onOptionsItemSelected 中的经典切换逻辑,并在 Eclipse 调试器中收到 ActivityNotFoundException 异常。

通过以“setIntent方式”处理菜单项,没有抛出异常。尽管如果我查看 LogCat,我可能会发现一个 MenuItemImpl: Can't find Activity to handle Intent;忽略

Ok. The solution was dumb enough. The destination activity name (say MyActivity_3 in the example) was mispelled in the manifest.

I changed the 3rd menu item handling to the classic switch logic in onOptionsItemSelected and I got an ActivityNotFoundException exception in Eclipse debugger.

By handling the menu item in the "setIntent way", no exception was thrown. Although if I looked at the LogCat, I could have spotted a MenuItemImpl: Can't find activity to handle intent; ignoring.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文