Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect the click event of home button in android (application launcher icon)

How to identify the click event in the application launcher icon in android? I need to go to home screen once the user click on this icon. For example, assume this is the manifest file:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
.......... 

Using the following code segment in main activity inside onCreate()

actionBar=getActionBar();
actionBar.setHomeButtonEnabled(true);

the application icon is click-able .I don't' have any way to detect the click event of this. Is this possible to do in android. Any suggestion to do that?

like image 559
Zusee Weekin Avatar asked Nov 24 '25 19:11

Zusee Weekin


1 Answers

You have to override onOptionsItemSelected(). Try this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // do what you want to be done on home button click event
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

Additional details check on Android Developers: "User Interface. Action Bar".

like image 113
Yehor Nemov Avatar answered Nov 26 '25 08:11

Yehor Nemov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!