问题
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