Display back button of action bar is not going back in Android
Add the following to your activity.You have to handle the click event of the back button.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
I would suggest not to handle "android.R.id.home" in onOptionsItemSelected as it is brittle. Rather you should override onSupportNavigateUp
method.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Note: If you are using onOptionsItemSelected
, then you should return false as default otherwise onSupportNavigateUp
method is not called.
Here you have 2 options:
a) provide a parentActivityName
to your SecondActivity
tag in AndroidManifest.xml like this:
<activity
...
android:name=".SecondActivity"
android:parentActivityName=".MainActivity" >
b) override onOptionsItemSelected
in SecondActivity
like this:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
I would suggest reading this guide for more information.