I've set my action bar like so but nothing happens when I click the home up button.
The two options below are enabled so shouldn't it go to the home activity automatically?
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
Action Bar
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
final String[] activities;
Resources res = getResources();
activities = res.getStringArray(R.array.activities);
ActionBar ab = getActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
ab.setTitle(R.string.app_name);
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ab.show();
/** Create an array adapter to populate dropdownlist */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, activities);
/** Setting dropdown items and item navigation listener for the actionbar */
getActionBar().setListNavigationCallbacks(adapter, navigationListener);
/** Enabling dropdown list navigation for the action bar */
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
return true;
}
As other people have said, the behaviour doesn't happen automatically - you need to tell it where to go.
However, I need to add another answer, as the current answers are all breaking Android design guidelines - Back != Home. See the documentation
What you really want to be doing is something along the lines of this:
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
Intent homeIntent = new Intent(this, HomeActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return (super.onOptionsItemSelected(menuItem));
}
Which will take you to the parent activity, rather than just go through the back stack. I've also added the Intent.Flag
to clear the back stack, it's a useful one to have when going to a home activity and can stop the back stack getting in a muddle when your users are using the 'Up' button
You also need to make sure your App knows what to do when it is pressed:
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
// ProjectsActivity is my 'home' activity
super. onBackPressed();
return true;
}
return (super.onOptionsItemSelected(menuItem));
}
We have to define meta data into our child activity in AndroidManifast.xml file as per given in official document:
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- The meta-data element is needed for versions lower than 4.1 -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
There is no need to define meta data if version is 4.1 or above and you have to enable your action bar home button as you have done in your code. No need to use back button code and it is working fine with my android app: Helper+
You need to define what happens here:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
}
return true;
}
You can achieve this using below one method although there are lots of ways to do so.
put this line inside your onCreate
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
override method onSupportNavigateUp
in your activity
@Override
public boolean onSupportNavigateUp() {
finish();
return super.onSupportNavigateUp();
}
Add these lines in your code:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
System.out.println("Pressed Back Button");
break;
default:
return super.onOptionsItemSelected(item);
}
return false;
}