Reputation: 118
I have a fragment that contains menu I have tried calling setHasOptionsMenu(true);
in both onCreate()
as well as onCreateView()
and set a toolbar
as my Actionbar
. I have gone through all the questions about the same here but none of the solutions seem to be working for me! I have tried changing the theme, menu.clear(), and returning true from onCreateOptions()
of the Activity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
// Inflating view layout
layout = inflater.inflate(R.layout.side_panel, container,false);
toolbar = layout.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
return layout;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_side_panel,menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
Upvotes: 0
Views: 641
Reputation: 1002
If you have declared Toolbar in the XML file, Then try this First get a reference to Toolbar using findViewByid() then inflate your menu
mToolbar.inflateMenu(R.menu.main_menu);
Upvotes: 2