android - What is the flow like for options menu when we add them in fragment and the main activity? -
i learning android , bit unsure how menu options work.here set have,
i have main activity has fragment inside it. main activity's oncreateoptionsmenu looks this
@override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } this menu_main.xml has settings button. supposed add refresh button in fragment created new menu xml , added code in fragment class
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); sethasoptionsmenu(true); } @override public void oncreateoptionsmenu(menu menu, menuinflater inflater) { inflater.inflate(r.menu.tempfragment, menu); } when ran app , did longpress, saw both refresh , setting button.
my question when have menu options main activity , associated fragment, flow like? combine both of menus? can see combining bit unsure regarding how getting handled internally? can explain me?
edit here menu file fragment
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_refresh" android:title="action_refresh" app:showasaction="never" /> </menu>
if have mainactivity inflates options menu, menu_main.xml, options menu present on fragment mainactivity creates.
within each fragment code, can create options menus specific fragment. inflate menu_friends.xml in friendsfragment. when instance of friendsfragment created mainactivity, both menu_main.xml and menu_friends.xml shown. can repeat fragments.
edit: @kartheek incorrect answer regarding calling sethasoptionsmenu(true) in fragment cancelling out activity's menu. statement allow menu created fragment. both menus included. see here: calling sethasoptionsmenu(true) fragment results in multiple calls oncreateoptionsmenu in activity
Comments
Post a Comment