user1847544
user1847544

Reputation: 977

How can I get Android ActionBar to goto Home when I click upper left?

I am seeing the up caret but nothing happens when I click it. Where do i tell it to goto home and set which activity is Home?

Upvotes: 2

Views: 5663

Answers (3)

iagreen
iagreen

Reputation: 32016

Updated to address comments below, which correctly point out my original answer was not correct for all cases. The "home/up" button have are supposed to take you the Home Activity or the Parent Activity, respectively. From the ActionBar documents --

By default, your application icon appears in the action bar on the left side. If you'd like, you can enable the icon to behave as an action item. In response to user action on the icon, your application should do one of two things:

Go to the application "home" activity, or
Navigate "up" the application's structural hierarchy

Implementing "Home" is straightforward. Launch an intent to the home activity (usually your main launch activity) of your app, clearing the backstack to the existing instance if one exists--

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent intent = new Intent(this, HomeActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

"up" is similar, except you should use

getActionBar().setDisplayHomeAsUpEnabled(true);

in your onCreate to get the icon to show as "up" instead of "home", and launch an instance of the parent activity.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent parentActivityIntent = new Intent(this, MyParentActivity.class);
            parentActivityIntent.addFlags(
                    Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(parentActivityIntent);
            finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

In both case, for Activities that can be launched from outside your application, you should add FLAG_ACTIVITY_NEW_TASK to launch into a new task instead of the callers task.

"Up" can get more complicated if your Activity can be launched from another app, and want to build a back stack for your application to get back to the root. See for this guide for more info.

My original answer, shown below, is considered bad form in the guide because it it treats the home/up button as 'back'. It will take you to the activity that invoked this activity, which is not necessarily the home or parent. It works well as 'up' for apps that have strict tree hierarchy of activities because an given activity was always launched by its parent.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Upvotes: 3

A--C
A--C

Reputation: 36449

You need the line:

getActionBar().setDisplayHomeAsUpEnabled(true);

put that in onCreate();

That will make it clickable. The you will need to handle the click event. This is done by overriding onOptionsItemSelected()

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                case android.R.id.home:
                 finish();
                default: 
                 return super.onOptionsItemSelected(item);      
                }
        }

If you want to use NavUtils, (which I have seen used by the ADT plugin when it makes Activities) you can replace change the implementation like so:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
              NavUtils.navigateUpFromSameTask(this);
                    return true;
        default: 
                      return super.onOptionsItemSelected(item); 
        }
    }

Don't forget to

import android.support.v4.app.NavUtils;

Also, you can add this to your Activity node:

    <activity
        android:name=".SomeActivity"
        android:label="@string/activity_label" >
         <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.mypackage.MainActivity" />
    </activity>

NavUtils is usually the better approach. For more information, see the official Android Guidelines.

Upvotes: 10

gosr
gosr

Reputation: 4708

In your onCreate you have:

getActionBar().setDisplayHomeAsUpEnabled(true);

And then use:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Upvotes: 5

Related Questions