Duplicate android:id in Menu File
ID |
java.android_matching_menu_id |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
android, menu |
Description
Reports <item> elements in an Android menu XML file that declare the same android:id as another item in the same file. Duplicate menu ids break onOptionsItemSelected dispatch — only the first match is reachable, so the rest are silently dead code.
Rationale
Each menu item must be uniquely addressable via its id so that the action handler (onOptionsItemSelected, NavigationUI.onNavDestinationSelected, etc.) can route the tap to the correct branch. With duplicate ids, the first item that matches the id wins and every subsequent click that should have gone to a sibling item is misrouted or dropped.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_save" />
<!-- Bad: duplicates action_save -->
<item android:id="@+id/action_save" />
<!-- Good: distinct id -->
<item android:id="@+id/action_share" />
</menu>
Remediation
Give every menu item a unique id (typically reflecting its action — action_save, action_share, action_export). When two items conceptually represent the same action in different states, model them as a single item with a state-aware title rather than as two items with the same id.