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.
try this,
And in
onCreate
add this line to make the options appear in yourToolbar
Your already have the autogenerated file res/menu/menu.xml defining action_settings.
In your MainActivity.java have the following methods:
In the
onCreateView()
method of your Fragment call:and also add these 2 methods:
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:
Call
inside
of Fragment
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:
I hope this answer can be helpful for someone with the same problem.