Reputation: 359
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.home:
onBackPressed();
Log.d("on","back pressed");
return true;
}
return super.onOptionsItemSelected(item);
}
Not able to go back when clicking on back arrow in action bar.Not even the method is not called. My class extends AppCompatActivity. Anybody plz help me to solve this issue
Upvotes: 1
Views: 132
Reputation: 292
starting from enabling the back button, don't if you are using the AppCompatActivity, if you are using same then use :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Listen for click events on android.R.id.home as seen you are using R.id.home replace that like this :
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
// if your previous activity is present in stack(Means Not finished) then prefer finish();
finish();
//Or You can use same method you mention
onBackPressed();
}
return super.onOptionsItemSelected(menuItem);
}
Upvotes: 0
Reputation: 284
Try these code Once`
@Override
public boolean onSupportNavigateUp() {
finish();
return true;
}
@Override
public void onBackPressed() {
finish();
}
`
Upvotes: 0
Reputation: 163
Are you sure you created Menu of BackButton.. because if not thats why it's not working.. may be you created Imagebutton or Imageview for Back
Upvotes: 0
Reputation: 31
The correct item ID for the back button is android.R.id.home
, once you change R.id.home
to android.R.id.home
, the code should work as expected.
Upvotes: 1