Action Bar's onClick listener for the Home button

AndroidAndroid IntentActionbarsherlockOnitemclicklistener

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

I hope this work for you ~~~ good luck

Solution 3 - Android

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

Solution 4 - Android

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!

Solution 5 - Android

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>

Solution 6 - Android

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).

Solution 7 - Android

Best way to customize Action bar onClickListener is onSupportNavigateUp()

This code will be helpful link for helping code

Solution 8 - Android

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;

    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnolomanView Question on Stackoverflow
Solution 1 - AndroidSaad MahmudView Answer on Stackoverflow
Solution 2 - Androidlynn8570View Answer on Stackoverflow
Solution 3 - AndroidrajuView Answer on Stackoverflow
Solution 4 - AndroidnolomanView Answer on Stackoverflow
Solution 5 - AndroidDavidView Answer on Stackoverflow
Solution 6 - AndroidNikolay ElenkovView Answer on Stackoverflow
Solution 7 - AndroidNaeem IbrahimView Answer on Stackoverflow
Solution 8 - AndroidSoha SohaView Answer on Stackoverflow