Action Bar的Home按钮的onClick监听器

89

如何为操作栏的主页按钮实现自定义onClickListener

我已经执行了 getSupportActionBar().setDisplayHomeAsUpEnabled(true);,现在我想在点击主页按钮时将用户重定向到特定的活动页面。

我尝试过以下内容:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    Intent i = new Intent();
                    i.setClass(BestemmingActivity.this, StartActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                    return true;
                }
            });
        default:
            return super.onOptionsItemSelected(item);
        }
    }

但它从未进入onMenuItemClick

基本上,它的做法就像在这个链接中所示,但仍然没有进入监听器。

9个回答

124

如果其他人需要解决方案

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == android.R.id.home) {
        onBackPressed();  return true;
    }

    return super.onOptionsItemSelected(item);
}

这应该是一个解决方案。非常好的答案。 - klaudiusnaban
3
在我的情况下,使用 return true; 起作用了。 - Rick
1
if 块的末尾加上 return true;,那么它就正确了。 - androidguy
4
这种方法在带有箭头图标的主页/返回按钮上不起作用。 - Trancer

111

我使用actionBarSherlock, 当我们设置supportActionBar.setHomeButtonEnabled(true);
我们可以覆写onMenuItemSelected方法:

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {

    int itemId = item.getItemId();
    switch (itemId) {
    case android.R.id.home:
        toggle();

        // Toast.makeText(this, "home pressed", Toast.LENGTH_LONG).show();
        break;

    }

    return true;
}

4
无法使用。因为 onMenuItemSelected 是 final 的,所以无法覆盖(override)它。 - Aritra Roy
使用Android注释,只需使用@OptionsItem(android.R.id.home) public void yourMethod() { } - Italo Borssatto

24

如果我们使用系统给定的操作栏,以下代码可以正常工作。

getActionBar().setHomeButtonEnabled(true);

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {

    int itemId = item.getItemId();
    switch (itemId) {
    case android.R.id.home:
      //do your action here.
        break;

    }

    return true;
}

7

修复:无需使用setOnMenuItemClickListener。 只需按下按钮,通过意图创建并启动活动。

非常感谢大家的帮助!


没错,操作栏会处理菜单监听器并自动调用onOptionsItemSelected()。不需要手动安装(这实际上可能会破坏某些东西)。 - Nikolay Elenkov

7

答案就在所发生事件的一半中。如果onOptionsItemSelected没有控制homeAsUp按钮,当父活动在manifest.xml设置时,系统会进入父活动。在活动标签中使用以下方式:


<activity ... >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.activities.MainActivity" /> 
</activity>

3

如果在ICS上运行,您需要明确启用主页操作。来自文档:

注意:如果您使用图标导航到主页活动,请注意从Android 4.0(API级别14)开始,您必须通过调用setHomeButtonEnabled(true)显式启用图标作为操作项(在早期版本中,该图标默认情况下已启用为操作项)。


4
我已经执行了 getSupportActionBar().setDisplayHomeAsUpEnabled(true);getSupportActionBar().setHomeButtonEnabled(true); - noloman

2

定制ActionBar的最佳方式是使用onSupportNavigateUp()函数实现OnClickListener

以下代码可能会有所帮助 链接到帮助代码


0

添加

android:parentActivityName=".NameOfParentActivity" 

将文本翻译成中文:显示设置的活动(在AndroidManifest.xml中)。

-4

你应该删除你的Override onOptionsItemSelected方法,并用以下代码替换你的onCreateOptionsMenu方法

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_action_bar_finish_order_stop, menu);
        menu.getItem(0).setOnMenuItemClickListener(new FinishOrderStopListener(this, getApplication(), selectedChild));
        return true;

    }

1
请添加一些解释,以帮助人们理解并阅读stackoverflow关于仅包含代码答案的政策。 - N0un

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接