overridefunonOptionsItemSelected(item:MenuItem)=when(item.itemId){R.id.action_settings->{// User chooses the "Settings" item. Show the app settings UI.true}R.id.action_favorite->{// User chooses the "Favorite" action. Mark the current item as a// favorite.true}else->{// The user's action isn't recognized.// Invoke the superclass to handle it.super.onOptionsItemSelected(item)}}
Java
@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){switch(item.getItemId()){caseR.id.action_settings:// User chooses the "Settings" item. Show the app settings UI.returntrue;caseR.id.action_favorite:// User chooses the "Favorite" action. Mark the current item as a// favorite.returntrue;default:// The user's action isn't recognized.// Invoke the superclass to handle it.returnsuper.onOptionsItemSelected(item);}}
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Add and handle actions\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add components in Compose. \n[Dynamic top app bar →](/develop/ui/compose/components/app-bars-dynamic) \n\nThe app bar lets you add buttons for user actions. This feature lets you put\nthe most important *actions* for the current context at the top of the app.\nFor example, a photo browsing app might show *share* and *create\nalbum* buttons at the top when the user is looking at their photo roll. When\nthe user looks at an individual photo, the app might show *crop* and\n*filter* buttons.\n\nSpace in the app bar is limited. If an app declares more actions than can fit\nin the app bar, the app bar sends the excess actions to an *overflow* menu.\nThe app can also specify that an action always shows in the overflow menu,\ninstead of displaying on the app bar.\n**Figure 1.** An action icon in the \"Now in Android\" app.\n\nAdd action buttons\n------------------\n\nAll action buttons and other items available in the action overflow are\ndefined in an XML\n[menu resource](/guide/topics/resources/menu-resource). To add\nactions to the action bar, create a new XML file in your project's\n`res/menu/` directory.\n\nAdd an\n[`\u003citem\u003e`](/guide/topics/resources/menu-resource#item-element)\nelement for each item you want to include in the action bar, as shown in the\nfollowing sample menu XML file: \n\n```xml\n\u003cmenu xmlns:android=\"http://schemas.android.com/apk/res/android\" \nxmlns:app=\"http://schemas.android.com/apk/res-auto\"\u003e\n\n \u003c!-- \"Mark Favorite\", must appear as action button if possible. --\u003e\n \u003citem\n android:id=\"@+id/action_favorite\"\n android:icon=\"@drawable/ic_favorite_black_48dp\"\n android:title=\"@string/action_favorite\"\n app:showAsAction=\"ifRoom\"/\u003e\n\n \u003c!-- Settings, must always be in the overflow. --\u003e\n \u003citem android:id=\"@+id/action_settings\"\n android:title=\"@string/action_settings\"\n app:showAsAction=\"never\"/\u003e\n\n\u003c/menu\u003e\n```\n\nThe `app:showAsAction` attribute specifies whether the action is\nshown as a button on the app bar. If you set\n`app:showAsAction=\"ifRoom\"`---as in the example code's\n*favorite* action---the action displays as a button if there is room in\nthe app bar for it. If there isn't enough room, excess actions are sent to the\noverflow menu. If you set `app:showAsAction=\"never\"`---as in the\nexample code's *settings* action---the action is always listed in the\noverflow menu and not displayed in the app bar.\n\nThe system uses the action's icon as the action button if the action displays\nin the app bar. You can find many useful icons in\n[Material Icons](https://www.google.com/design/icons/).\n\nRespond to actions\n------------------\n\nWhen the user selects one of the app bar items, the system calls your\nactivity's\n[onOptionsItemSelected()](/reference/android/app/Activity#onOptionsItemSelected(android.view.MenuItem))\ncallback method and passes a\n[MenuItem](/reference/android/view/MenuItem) object\nto indicate which item was tapped. In your implementation of\n`onOptionsItemSelected()`, call the\n[MenuItem.getItemId()](/reference/android/view/MenuItem#getItemId())\nmethod to determine which item was tapped. The ID returned matches the value you\ndeclare in the corresponding `\u003citem\u003e` element's\n`android:id` attribute.\n\nFor example, the following code snippet checks which action the user selects.\nIf the method doesn't recognize the user's action, it invokes the superclass\nmethod: \n\n### Kotlin\n\n```kotlin\noverride fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {\n R.id.action_settings -\u003e {\n // User chooses the \"Settings\" item. Show the app settings UI.\n true\n }\n\n R.id.action_favorite -\u003e {\n // User chooses the \"Favorite\" action. Mark the current item as a\n // favorite.\n true\n }\n\n else -\u003e {\n // The user's action isn't recognized.\n // Invoke the superclass to handle it.\n super.onOptionsItemSelected(item)\n }\n}\n```\n\n### Java\n\n```java\n@Override\npublic boolean onOptionsItemSelected(MenuItem item) {\n switch (item.getItemId()) {\n case R.id.action_settings:\n // User chooses the \"Settings\" item. Show the app settings UI.\n return true;\n\n case R.id.action_favorite:\n // User chooses the \"Favorite\" action. Mark the current item as a\n // favorite.\n return true;\n\n default:\n // The user's action isn't recognized.\n // Invoke the superclass to handle it.\n return super.onOptionsItemSelected(item);\n\n }\n}\n```"]]