akkk akkk - 5 months ago 30
Android Question

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

Answer

the back button is nothing more than a menu item so you have to override onOptionsItemSelected

and look for when the button is pressed in there and do something like this

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