You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Toolbar添加菜单项后返回按钮失效问题求助

修复DetailActivity中Toolbar返回按钮失效的问题

先看一下我的DetailActivity界面:
Detail Activity界面

问题描述

我给这个Activity添加了自定义菜单项后,Toolbar上的返回按钮突然不能正常工作了;但只要把菜单项移除,返回按钮就恢复正常。以下是当前的Activity代码:

public class DetailActivity extends AppCompatActivity { 
    ImageView mImageView; 
    TextView mTitle; 
    TextView mDate; 
    TextView mDescription; 
    private String newsTitle; 
    private String newsImage; 
    private String newsDate; 
    private String newsDescription; 
    private static String NEWS_SHARE_HASHTAG ="#PopularMoviesApp"; 
    private String date1; 
    private String date2; 
    private String newsUrl; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_detail); 
        Toolbar toolbar = (Toolbar)findViewById(R.id.detail_toolbar); 
        setSupportActionBar(toolbar); 
        if(getSupportActionBar() != null){ 
            getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
        } 
        Intent i = getIntent(); 
        mImageView = (ImageView)findViewById(R.id.detail_image_view); 
        mTitle = (TextView)findViewById(R.id.detail_title); 
        mDate = (TextView)findViewById(R.id.detail_publish_date); 
        mDescription = (TextView)findViewById(R.id.detail_description); 
        newsImage = i.getStringExtra("image"); 
        newsTitle = i.getStringExtra("title"); 
        newsDate = i.getStringExtra("date"); 
        newsDescription = i.getStringExtra("description"); 
        newsUrl = i.getStringExtra("url"); 
        date1 = newsDate.substring(0,10); 
        date2 = newsDate.substring(11,19); 
        Picasso.with(this).load(newsImage) 
            .placeholder(R.drawable.ic_broken_image) 
            .into(mImageView); 
        mTitle.setText(newsTitle); 
        mDescription.setText(newsDescription); 
        mDate.setText(date2 + ", " + date1); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.detail_menu,menu); 
        return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
        super.onOptionsItemSelected(item); 
        if(item.getItemId() == R.id.detail_browser_btn){ 
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(newsUrl)); 
            startActivity(browserIntent); 
        }else if(item.getItemId() == R.id.detail_share_btn){ 
            Intent shareIntent = createShareNewsIntent(); 
            startActivity(shareIntent); 
        } 
        return true; 
    } 

    private Intent createShareNewsIntent() { 
        Intent shareIntent = ShareCompat.IntentBuilder.from(this) 
            .setType("text/plain") 
            .setText(mTitle + NEWS_SHARE_HASHTAG + "\n\n\n" + newsTitle + "\n\n\n" + newsDescription + "\n\n\n" + newsDate) 
            .getIntent(); 
        return shareIntent; 
    } 
}

问题原因

问题出在onOptionsItemSelected方法里:

  1. 你调用super.onOptionsItemSelected(item)的时机不对,而且没有利用它的返回值——父类方法会处理返回按钮(对应ID是android.R.id.home)的逻辑,但你只是调用了它,最后直接返回true,相当于覆盖了父类的处理结果。
  2. 你的代码里没有显式处理android.R.id.home这个返回按钮的ID,导致点击返回按钮时没有触发返回逻辑。

修复方案

你可以选择以下两种方式中的任意一种来修复:

方案一:显式处理返回按钮ID

修改onOptionsItemSelected方法,先判断是否是返回按钮,手动触发返回逻辑:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // 处理返回按钮
    if (item.getItemId() == android.R.id.home) {
        onBackPressed();
        return true;
    }
    // 处理自定义菜单项
    if(item.getItemId() == R.id.detail_browser_btn){
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(newsUrl));
        startActivity(browserIntent);
        return true;
    }else if(item.getItemId() == R.id.detail_share_btn){
        Intent shareIntent = createShareNewsIntent();
        startActivity(shareIntent);
        return true;
    }
    // 交给父类处理其他未覆盖的情况
    return super.onOptionsItemSelected(item);
}

方案二:优先让父类处理默认逻辑

调整super.onOptionsItemSelected(item)的位置,先让父类处理返回按钮这类默认item,如果父类处理了就直接返回:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // 先让父类处理默认item(比如返回按钮),如果父类处理了就直接返回
    if (super.onOptionsItemSelected(item)) {
        return true;
    }
    // 处理自定义菜单项
    if(item.getItemId() == R.id.detail_browser_btn){
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(newsUrl));
        startActivity(browserIntent);
    }else if(item.getItemId() == R.id.detail_share_btn){
        Intent shareIntent = createShareNewsIntent();
        startActivity(shareIntent);
    }
    return true;
}

这两种方式都能让返回按钮恢复正常工作,同时保留自定义菜单项的功能。

内容的提问来源于stack exchange,提问作者Theo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:25:01