Action Bar's onClick listener for the Home button
Asked Answered
C

9

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.

Criterion answered 18/6, 2012 at 9:4 Comment(0)
C
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!

Criterion answered 18/6, 2012 at 9:30 Comment(1)
Right, the action bar takes care of menu listeners and calls onOptionsItemSelected() automatically. No need to install manually (that can actually break things).Zara
E
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);
}
Esther answered 24/2, 2015 at 9:20 Comment(3)
Worked for me with return true;Journeywork
Put a return true; at the end of the if block, then it's correctCanine
This approach is not working for Home/Back button with arrow icon.Anticipation
E
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;
}
Epispastic answered 10/7, 2012 at 8:22 Comment(2)
Doesn't work me me. Cannot override onMenuItemSelected as its final.Ramberg
With android annotations just use @OptionsItem(android.R.id.home) public void yourMethod() { }Ontina
I
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;
}
Interdisciplinary answered 2/6, 2014 at 11:37 Comment(0)
C
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!

Criterion answered 18/6, 2012 at 9:30 Comment(1)
Right, the action bar takes care of menu listeners and calls onOptionsItemSelected() automatically. No need to install manually (that can actually break things).Zara
M
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>
Mattoid answered 10/7, 2016 at 7:38 Comment(0)
Z
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).

Zara answered 18/6, 2012 at 9:9 Comment(1)
I already did a getSupportActionBar().setDisplayHomeAsUpEnabled(true); and a getSupportActionBar().setHomeButtonEnabled(true);Criterion
W
2

Best way to customize Action bar onClickListener is onSupportNavigateUp()

This code will be helpful link for helping code

Welford answered 8/6, 2016 at 3:53 Comment(0)
Y
0

Add

android:parentActivityName=".NameOfParentActivity" 

to the activity (in AndroidManifest.xml) that displays the settings.

Your answered 12/10, 2023 at 18:47 Comment(0)
T
-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;

    }
Thermal answered 28/7, 2017 at 9:22 Comment(1)
Please add some explanations to help people to understand and go (back?) reading stackoverflow's policies about answer with only code.Sextuplet

© 2022 - 2024 — McMap. All rights reserved.