onCreateOptionsMenu inside Fragments
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Fragments can contribute menu items, but they do not participate in the activity menu automatically. The exact setup depends on whether you use the older fragment menu callbacks or the newer MenuHost and MenuProvider APIs, which are usually the cleaner lifecycle-aware approach in modern Android code.
The Classic Fragment Menu Pattern
In the older style, the fragment must opt in by calling setHasOptionsMenu(true).
Without setHasOptionsMenu(true), onCreateOptionsMenu inside the fragment will not be called.
The Modern MenuHost Approach
AndroidX introduced MenuHost and MenuProvider, which are usually a better fit for fragments because they are tied to lifecycle owners more explicitly.
This is often safer because the menu provider is scoped to the fragment view lifecycle rather than lingering after the view is destroyed.
Why Lifecycle Scope Matters
Fragments can have their views destroyed and recreated while the fragment object still exists. If menu handling is not tied to viewLifecycleOwner or another correct lifecycle boundary, stale callbacks can remain registered and cause duplicated or confusing menu behavior.
That is one reason the modern API is generally preferred.
Multiple Fragments and Menu Conflicts
In multi-fragment screens, more than one fragment may try to contribute actions. Keep menu item IDs distinct and be clear about which fragment should own which actions. If the toolbar really belongs to a child fragment rather than the activity, it may be cleaner to inflate and handle the menu directly on that fragment-owned toolbar.
This ownership question matters more than many developers expect. A lot of menu bugs come from unclear architecture rather than incorrect callback syntax, especially when navigation, nested fragments, and a shared activity toolbar all interact.
Once menu ownership is explicit, the API choice usually becomes obvious as well. Fragment-scoped menu actions work best when their lifecycle follows the fragment’s visible UI state instead of the host activity’s longer lifetime.
That alignment is what prevents stale or duplicated actions after navigation and view recreation.
Common Pitfalls
- Overriding
onCreateOptionsMenuin a fragment without enabling menu participation first in the legacy API. - Registering menu behavior against the wrong lifecycle, so callbacks survive longer than the fragment view.
- Letting multiple fragments contribute conflicting menu items with overlapping IDs.
- Assuming the activity will automatically route all menu events the way the fragment expects.
- Using the old pattern in new code when a lifecycle-aware
MenuProviderwould be simpler.
Summary
- Fragments do not participate in the options menu automatically.
- In the classic API, call
setHasOptionsMenu(true)and override the menu callbacks. - In modern Android code, prefer
MenuHostandMenuProviderfor lifecycle-aware menu handling. - Scope menu registration carefully to avoid stale callbacks after view recreation.
- Treat menu ownership as part of fragment architecture, not just a callback detail.
Related reading
- OnItemCLickListener not working in listview
- onMeasure custom view explanation
- onRequestPermissionsResult not being called in fragment if defined in both fragment and activity
- Open AppStore through button
- Open AppStore through button
- Open UIDocument synchronously
- Opening the Settings app from another app
- Opening view controller from app delegate using swift
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.