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

六眼飞鱼酱① 提交于 2019-12-11 00:54:12

问题


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?


回答1:


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




回答2:


try this in onCreate(..)

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

     View ic=((View) findViewById(android.R.id.home));
        ic.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Toast.makeText(getBaseContext(),"icon " , Toast.LENGTH_LONG).show();    
                                    //...               

            }
        }); 


来源:https://stackoverflow.com/questions/22754733/detect-the-click-event-of-home-button-in-android-application-launcher-icon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!