简体   繁体   English

在Android中的工具栏上单击后退按钮没有任何反应

[英]Nothing happens on back button click on Toolbar in Android

When back button is clicked on the Toolbar nothing happens. 在工具栏上单击“后退”按钮时,没有任何反应。 I dont know whats the problem. 我不知道是什么问题。

toolbar = (Toolbar) findViewById(R.id.include);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Notices For Students");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

You can do it by using this: 您可以使用以下方法做到这一点:

  toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

here i'm finishing when user click on back. 当用户单击背面时,我在这里完成。

Kindly use this and tell me if you may face any problem. 请使用此功能,并告诉我您是否可能遇到任何问题。

the back button is nothing more than a menu item so you have to override onOptionsItemSelected 后退按钮仅是菜单项,因此您必须覆盖onOptionsItemSelected

and look for when the button is pressed in there and do something like this 并寻找在其中按下按钮时执行的操作

if(menuItem.getItemId() == android.R.id.home){
    finish();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM