user3418460
user3418460

Reputation: 13

Listener Home and HomeAsUp buttons not works on Support Action Bar

I just use Support Action Bar from AppCompact v7. I'm wrote bar.setDisplayHomeAsUpEnabled(true) and want to listen this button, but in listener:

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    switch (id) {
    case R.id.action_settings:
        return true;
    case R.id.HomeAsUp:
        return true;
    default:
        break;
    } 

    return super.onOptionsItemSelected(item);
}

When I want to find item from it id, method return nothing. How to listen this buttons with Support Action Bar?

Upvotes: 0

Views: 803

Answers (1)

ceph3us
ceph3us

Reputation: 7474

    R.id.home - res id 2131034117
    R.id.homeAsUp - res id 2131034132
    android.R.id.home - res id 16908332  <-- HomeAsUpIndicator

ActionBar.setHomeAsUpIndicator(getResIdFromAttribute(this, R.attr.homeAsUpIndicator));
ActionBar.setDisplayHomeAsUpEnabled(true);

public static int getResIdFromAttribute(final Activity activity, final int attr) {
    if (attr == 0) return 0;
    final TypedValue typedvalueattr = new TypedValue();
    activity.getTheme().resolveAttribute(attr, typedvalueattr, true);
    return typedvalueattr.resourceId;
}

Upvotes: 1

Related Questions