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

时间:2018-05-21 08:03:30

标签: android android-toolbar

我的应用的详细活动如下所示。

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

如何解决这个问题?

4 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:3)

添加

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

onOptionsItemSelected覆盖。

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

应该这样做。

答案 2 :(得分:2)

尝试使用此代码:

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

答案 3 :(得分:1)

在itemselected方法中尝试这种方式

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

        }
        finish();
        return true;
    }