Reputation: 583
can someone explain me the difference between:
onMenuItemSelected (int featureId, MenuItem item)
http://developer.android.com/reference/android/app/Activity.html#onMenuItemSelected%28int,%20android.view.MenuItem%29
and
onOptionsItemSelected (MenuItem item)
http://developer.android.com/reference/android/app/Activity.html#onOptionsItemSelected%28android.view.MenuItem%29
in Android? I found a tutorial were someone overrides both methods.
// Reaction to the menu selection
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.insert:
createTodo();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.insert:
createTodo();
return true;
}
return super.onOptionsItemSelected(item);
}
Source: http://www.vogella.de/articles/AndroidSQLite/article.html
Upvotes: 42
Views: 25678
Reputation: 131
Using ADT 17 (version 4.2) the onOptionsItemSelected
callback will allow the user to access menu options from any context including the Menu Button and the Action Bar. As of Android version 3.0 the preferred method is the Action Bar which can be accessed from onMenuItemSelected
. If you are designing an app that supports versions 2.3 or earlier than onOptionsItemSelected
is the way you want to go.
Upvotes: 10
Reputation: 6716
Android knows about several types of menus (e.g. Options Menu and Context Menu). onMenuItemSelected
is the generic callback. You don't need to use this usually. onOptionsItemSelected
is the callback of the options menu and onContextItemSelected
is the callback of the context menu. Use these two specific ones instead.
Upvotes: 51
Reputation: 544
Looking at the code, onMenuItemSelected can be called by a Options Menu (Menu button) click or by Context Menu click. Basically it just forwards the clicks to the other corresponding methods.
Look at the code here: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/app/Activity.java#2078
Upvotes: 3