I have a toolbar as well as a navigation drawer. When I start my app, the toolbar and navigation drawer are created. When I click items in the navigation drawer, it starts n
I solved this issues correctly. My solution is as following code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mRootView = inflater.inflate(R.layout.fragment_order_history, container, false);
//getActivity().invalidateOptionsMenu();
setHasOptionsMenu(true);
return mRootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Inside XML add this line
app:menu="@menu/main_menu"
Override onCreateOptionsMenu
method in your every fragment.
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_menu_xml, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Add similar code to your fragments:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.library_fragment, parent, false);
setHasOptionsMenu(true);
return v;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_menu_xml, menu);
super.onCreateOptionsMenu(menu, inflater);
}
This way you can customize the menu for your fragments.