I have been trying to add a back button to the action bar.
I want my view to look like this:
I want to add the back button in the left of the action bar.
I added this code
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
but it doesn't work.
How can I fix this?
After setting
actionBar.setHomeButtonEnabled(true);
Add the following code:
Firstly Use this:
Then set operation of button click in
onOptionsItemSelected
methodAdd
and then add the following
As suggested by naXa I've added a check on the
itemId
, to have it work correctly in case there are multiple buttons on the action bar.Simpler and better: For API >= 16
Simply add "parentActivityName" for each activity in Manifest. The back button will automatically take u to the parent activity.
After setting
You have to configure the parent activity in your AndroidManifest.xml
Look here for more information http://developer.android.com/training/implementing-navigation/ancestral.html