akkk
akkk

Reputation: 1467

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

Upvotes: 1

Views: 1321

Answers (2)

Smit.Satodia
Smit.Satodia

Reputation: 597

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.

Upvotes: 3

tyczj
tyczj

Reputation: 73741

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

Upvotes: 1

Related Questions