Android 工具栏后退箭头不显示';行不通

Android 工具栏后退箭头不显示';行不通,android,toolbar,Android,Toolbar,我创建了其他活动,并希望使用工具栏返回箭头返回主要活动,但在代码下不工作,请帮助我 mToolbar = (Toolbar) findViewById(R.id.toolbar); mToolbar.setTitle(R.string.setting); setSupportActionBar(mToolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportAction

我创建了其他活动,并希望使用工具栏返回箭头返回主要活动,但在代码下不工作,请帮助我

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    mToolbar.setTitle(R.string.setting);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);  
    mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
                System.out.println("??");
                finish();
                return true;

            }
            return true;
        }
    });

}
尝试以下方法:

@Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
        case android.R.id.home:


            return true;

        }
            return (super.onOptionsItemSelected(menuItem));

        }
尝试以下方法:

@Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
        case android.R.id.home:


            return true;

        }
            return (super.onOptionsItemSelected(menuItem));

        }

工具栏有一个方法,即设置导航onClickListener。它允许您监听后退箭头上的单击事件。如果您想了解有关工具栏的更多信息,请参阅文档

 mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
   public void onClick(View v) {
            System.out.println("??");
            finish();
    }
});

工具栏有一个方法,即设置导航onClickListener。它允许您监听后退箭头上的单击事件。如果您想了解有关工具栏的更多信息,请参阅文档

 mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
   public void onClick(View v) {
            System.out.println("??");
            finish();
    }
});
您应该使用以下选项:

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case android.R.id.home:
            onBackPressed();
            return true;
    }
    return super.onOptionsItemSelected(item);
}
您应该使用以下选项:

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