Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android action bar home button

People also ask

How can I customize my action bar in Android?

This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Where is the action bar on an Android phone?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.


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