代码之家  ›  专栏  ›  技术社区  ›  Theo

工具栏的后退按钮不工作[重复]

  •  0
  • Theo  · 技术社区  · 6 年前

    我的应用程序的详细活动如下。

    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 回复  |  直到 6 年前
        1
  •  3
  •   magicleon94    6 年前

    添加

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

    给你的 onOptionsItemSelected 覆盖。

    这样,当您点击该按钮时,您将调用 onBackPressed 方法,这是返回的默认操作(比直接调用更干净的解决方案) finish() )

    这应该可以做到。

        2
  •  3
  •   MinnuKaAnae    6 年前
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
    
        3
  •  2
  •   Subrata    6 年前

    使用以下代码进行尝试:

     @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
        4
  •  1
  •   Hasmukh Kachhatiya    6 年前

    在itemselectedMethod中尝试此方法

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