I have a lower navigation and when I change from one fragment to another I want the top toolbar to change the options.
I have a lower navigation and when I change from one fragment to another I want the top toolbar to change the options.
In order to do this, you must call the setHasOptionsMenu function in the fragment, in the onCreateView function. Then you override the functions that add the options in the toolbar.
Example:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_layout_example, container, false); // TU LAYOUT
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_example,menu); // TU MENU
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.optAdd: // TU OPCION
return true;
default:
return super.onOptionsItemSelected(item);
}
}