onCreateOptionsMenu inside Fragments
Asked Answered
C

7

205

I have placed setHasOptionsMenu(true) inside onCreateView, but I still can't call onCreateOptionsMenu inside fragments.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                         Bundle savedInstanceState) {   
   setHasOptionsMenu(true);             
   return inflater.inflate(R.layout.facesheet, container, false);
}

Below is my onCreateOptionsMenu code.

@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    getSupportMenuInflater().inflate(R.menu.layout, menu);
    return (super.onCreateOptionsMenu(menu));
}

The error message I get:

The method onCreateOptionsMenu(Menu) of type Fragment must override or implement a supertype method.

Carolanncarole answered 27/3, 2013 at 7:31 Comment(1)
Thanks for the 'setHasOptionsMenu(true);', I was looking for that.Concent
P
554

try this,

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_sample, menu);
    super.onCreateOptionsMenu(menu,inflater);
}

Finally, in onCreateView method, add this line to make the options appear in your Toolbar

setHasOptionsMenu(true);
Pericynthion answered 27/3, 2013 at 7:54 Comment(7)
For some reason onCreateOptionsMenu is not called in my FragmentWhitfield
it won't be called if you don't add this line: setHasOptionsMenu(true);Injector
onCreateOptionsMenu() for fragments has different arguments than for Activities.Depict
and also a different return type as void unlike boolean of onCreateOptionsMenu() in activity as mentioned on above ans.Youngstown
Interesting note: if you also override onCreateOptionsMenu in your containing Activity, both options menu items will be displayed.Illogic
setHasOptionsMenu(true); has to be called in onCreate() to be complete.Hyaloplasm
How to avoid show "action_settings" in fragment options?Milligram
H
24

Your already have the autogenerated file res/menu/menu.xml defining action_settings.

In your MainActivity.java have the following methods:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_settings:
            // do stuff, like showing settings fragment
            return true;
    }

    return super.onOptionsItemSelected(item); // important line
}

In the onCreateView() method of your Fragment call:

setHasOptionsMenu(true); 

and also add these 2 methods:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.fragment_menu, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_1:
            // do stuff
            return true;

        case R.id.action_2:
            // do more stuff
            return true;
    }

    return false;
}

Finally, add the new file res/menu/fragment_menu.xml defining action_1 and action_2.

This way when your app displays the Fragment, its menu will contain 3 entries:

  • action_1 from res/menu/fragment_menu.xml
  • action_2 from res/menu/fragment_menu.xml
  • action_settings from res/menu/menu.xml
Hereafter answered 11/7, 2015 at 17:50 Comment(3)
the question was inside fragments, not activityGrosberg
@Grosberg the answer does explain the fragment menu. I appreciated the explanation of how it interacts with the activity menu.Cookstove
How to avoid show "action_settings" in fragment options?Milligram
G
14

I tried the @Alexander Farber and @Sino Raj answers. Both answers are nice, but I couldn't use the onCreateOptionsMenu inside my fragment, until I discover what was missing:

Add setSupportActionBar(toolbar) in my Activity, like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.id.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

I hope this answer can be helpful for someone with the same problem.

Greening answered 27/5, 2016 at 0:18 Comment(0)
I
6

Call

setSupportActionBar(toolbar)

inside

onViewCreated(...) 

of Fragment

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
    ((MainActivity)getActivity()).setSupportActionBar(toolbar);
    setHasOptionsMenu(true);
}
Instrument answered 13/12, 2017 at 8:26 Comment(1)
Thank you fixed my problemMig
C
4

Set setHasMenuOptions(true) works if application has a theme with Actionbar such as Theme.MaterialComponents.DayNight.DarkActionBar or Activity has it's own Toolbar, otherwise onCreateOptionsMenu in fragment does not get called.

If you want to use standalone Toolbar you either need to get activity and set your Toolbar as support action bar with

(requireActivity() as? MainActivity)?.setSupportActionBar(toolbar)

which lets your fragment onCreateOptionsMenu to be called.

Other alternative is, you can inflate your Toolbar's own menu with toolbar.inflateMenu(R.menu.YOUR_MENU) and item listener with

toolbar.setOnMenuItemClickListener {
   // do something
   true
}
Carliecarlile answered 10/9, 2020 at 18:1 Comment(0)
A
1
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_add_customer, container, false);
        setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_sample, menu);
    super.onCreateOptionsMenu(menu,inflater);
}
Alissaalistair answered 19/7, 2017 at 9:3 Comment(0)
D
0

You can do easily as, inside your fragment on Create method,

setHasOptionsMenu(true)

and now you can override,

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    super.onCreateOptionsMenu(menu, inflater)
    menu.clear()
}
Dexterous answered 2/12, 2021 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.