OnClick action bar for home button

How can I implement a custom onClickListener for the home button of the action bar?

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

I tried:

 @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 onMenuItemClick .

In principle, this is done in the same way as in this link , but still it is not included in the listener.

+69
android android-intent actionbarsherlock onitemclicklistener
Jun 18 2018-12-12T00:
source share
8 answers

Bugfix: no need to use setOnMenuItemClickListener . Just by pressing a button, it creates and launches an action through an intention.

Thank you all for your help!

+7
Jun 18 '12 at 9:30
source share

I use 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 is for you ~~~ good luck

+106
Jul 10 '12 at 8:22
source share

if anyone else needs a solution

 @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { onBackPressed(); return true; } return super.onOptionsItemSelected(item); } 
+86
Feb 24 '15 at 9:20
source share

if we use the system action bar, the 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; } 
+20
Jun 02 '14 at 11:37
source share

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

Note. If you use the icon to transition to home activity, be aware that starting with Android 4.0 (API level 14), you must explicitly include the icon as an action element by calling setHomeButtonEnabled (true) (in previous versions, the icon was included as an action element by default).

+3
Jun 18 2018-12-12T00:
source share

The best way to customize the onClickListener action bar is onSupportNavigateUp ()

This code will be useful to get the code.

+2
Jun 08 '16 at 3:53 on
source share

responsible in half the cases. if onOptionsItemSelected does not control the homeAsUp button when the parent activity is set to manifest.xml, the system goes to the parent activity. use this in an activity tag:

 <activity ... > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.activities.MainActivity" /> </activity> 
+1
Jul 10 '16 at 7:38
source share

you must remove your Override onOptionsItemSelected and replace it 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; } 
-2
Jul 28 '17 at 9:22
source share



All Articles