89

How can I implement a custom onClickListener for the Home button of the Action Bar?

I already did a getSupportActionBar().setDisplayHomeAsUpEnabled(true); and now I want to redirect the user to a certain activity in case the Home button is clicked.

I tried with:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    Intent i = new Intent();
                    i.setClass(BestemmingActivity.this, StartActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                    return true;
                }
            });
        default:
            return super.onOptionsItemSelected(item);
        }
    }

but it never enters in the onMenuItemClick.

Basically, it's done just like in this link but still it doesn't enter in the listener.

GAMA
  • 5,958
  • 14
  • 79
  • 126
noloman
  • 11,411
  • 20
  • 82
  • 129

8 Answers8

124

if anyone else need the solution

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == android.R.id.home) {
        onBackPressed();  return true;
    }

    return super.onOptionsItemSelected(item);
}
Sk Saad Al Mahmud
  • 2,984
  • 2
  • 24
  • 22
111

I use the actionBarSherlock, after we set supportActionBar.setHomeButtonEnabled(true);
we can override the onMenuItemSelected method:

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {

    int itemId = item.getItemId();
    switch (itemId) {
    case android.R.id.home:
        toggle();

        // Toast.makeText(this, "home pressed", Toast.LENGTH_LONG).show();
        break;

    }

    return true;
}
starball
  • 20,030
  • 7
  • 43
  • 238
lynn8570
  • 1,685
  • 1
  • 14
  • 24
24

if we use the system given action bar following code works fine

getActionBar().setHomeButtonEnabled(true);

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {

    int itemId = item.getItemId();
    switch (itemId) {
    case android.R.id.home:
      //do your action here.
        break;

    }

    return true;
}
raju
  • 1,254
  • 13
  • 18
7

Fixed: no need to use a setOnMenuItemClickListener. Just pressing the button, it creates and launches the activity through the intent.

Thanks a lot everybody for your help!

noloman
  • 11,411
  • 20
  • 82
  • 129
  • Right, the action bar takes care of menu listeners and calls `onOptionsItemSelected()` automatically. No need to install manually (that can actually break things). – Nikolay Elenkov Jun 18 '12 at 12:26
7

answers in half part of what is happening. if onOptionsItemSelected not control homeAsUp button when parent activity sets in manifest.xml system goes to parent activity. use like this in activity tag:

<activity ... >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.activities.MainActivity" /> 
</activity>
David
  • 2,129
  • 25
  • 34
3

You need to explicitly enable the home action if running on ICS. From the docs:

Note: If you're using the icon to navigate to the home activity, beware that beginning with Android 4.0 (API level 14), you must explicitly enable the icon as an action item by calling setHomeButtonEnabled(true) (in previous versions, the icon was enabled as an action item by default).

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • 4
    I already did a `getSupportActionBar().setDisplayHomeAsUpEnabled(true);` and a `getSupportActionBar().setHomeButtonEnabled(true);` – noloman Jun 18 '12 at 09:12
2

Best way to customize Action bar onClickListener is onSupportNavigateUp()

This code will be helpful link for helping code

Community
  • 1
  • 1
Naeem Ibrahim
  • 3,375
  • 1
  • 21
  • 21
-4

you should to delete your the Override onOptionsItemSelected and replate your onCreateOptionsMenu with this code

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_action_bar_finish_order_stop, menu);
        menu.getItem(0).setOnMenuItemClickListener(new FinishOrderStopListener(this, getApplication(), selectedChild));
        return true;

    }
Soha Soha
  • 1
  • 1
  • 1
    Please add some explanations to help people to understand and go (back?) reading stackoverflow's policies about answer with only code. – N0un Jul 28 '17 at 09:30