【问题标题】:Back button of Toolbar doesn't work [duplicate]工具栏的后退按钮不起作用[重复]
【发布时间】:2018-05-21 08:03:29
【问题描述】:

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

当我添加菜单项时,工具栏的后退按钮不起作用。如果我把它们拿出来,一切正常。这是我针对该特定活动的代码。

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

如何解决这个问题?

【问题讨论】:

    标签: android android-toolbar


    【解决方案1】:
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
    

    【讨论】:

      【解决方案2】:

      添加

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

      到您的onOptionsItemSelected 覆盖。

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

      应该可以的。

      【讨论】:

      • 这是一个很好的答案,但我认为这不是专业的方法......
      • 什么意思?
      【解决方案3】:

      用这段代码试试吧:

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

      【讨论】:

        【解决方案4】:

        在 itemselectedmethod 中尝试这种方式

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

        【讨论】:

          猜你喜欢
          • 2013-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多