项目中遇到onContextItemSelected不会被调用的问题。
先看android本身的源码 :
/**
* Default implementation of
* {@link android.view.Window.Callback#onMenuItemSelected}
* for activities. This calls through to the new
* {@link #onOptionsItemSelected} method for the
* {@link android.view.Window#FEATURE_OPTIONS_PANEL}
* panel, so that subclasses of
* Activity don't need to deal with feature codes.
*/
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (featureId) {
case Window.FEATURE_OPTIONS_PANEL:
// Put event logging here so it gets called even if subclass
// doesn't call through to superclass's implmeentation of each
// of these methods below
EventLog.writeEvent(50000, 0, item.getTitleCondensed());
if (onOptionsItemSelected(item)) {
return true;
}
if (mFragments.dispatchOptionsItemSelected(item)) {
return true;
}
if (item.getItemId() == android.R.id.home && mActionBar != null &&
(mActionBar.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
if (mParent == null) {
return onNavigateUp();
} else {
return mParent.onNavigateUpFromChild(this);
}
}
return false;
case Window.FEATURE_CONTEXT_MENU:
EventLog.writeEvent(50000, 1, item.getTitleCondensed());
if (onContextItemSelected(item)) {
return true;
}
return mFragments.dispatchContextItemSelected(item);
default:
return false;
}
}
当你点击某个菜单(上下文菜单、actionBar菜单、2.2的下文菜单)都会先调用onMenuItemSelected,然后根据是哪种类型去调用相应的方法onOptionsItemSelected或者onContextItemSelected。
我出现问题是因为我的onMenuItemSelected方法是继承自sherlock类的,它写的方法有问题,导致不会去调用onContextItemSelected方法。
@Override
public boolean onMenuItemSelected(int featureId,android.view.MenuItem item) {
if (featureId == Window.FEATURE_OPTIONS_PANEL) {
return onOptionsItemSelected(item);
}else if (featureId == Window.FEATURE_CONTEXT_MENU) {
return onContextItemSelected(item);
}
return false;
}
这是已修复好的~~~
所以,理清安卓菜单事件的响应顺序~~~