工具栏的后退按钮不起作用

Back button of Toolbar doesn't work

我的应用程序的详细 [​​=20=] 如下所示。

当我添加菜单项时,工具栏的后退按钮不起作用。如果我把它们拿出来,一切正常。这是我针对特定 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;
}
}

如何解决这个问题?

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id == android.R.id.home){
        finish();
    }
    return super.onOptionsItemSelected(item);
}

在itemselectedmethod中试试这个方法

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.homeAsUp:
                finish();
                return true;

        }
        finish();
        return true;
    }

添加

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

您的 onOptionsItemSelected 覆盖。

这样,当您点击该按钮时,您将调用 onBackPressed 方法,这是返回的默认操作(比粗暴地调用 finish() 更简洁的解决方案)

应该可以了。

用这段代码试试看:

 @Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
        case android.R.id.home:
            onBackPressed();
            return true;
    }
    return super.onOptionsItemSelected(item);
}