How to change toolbar when changing fragment in Android? [closed]

0

I have a lower navigation and when I change from one fragment to another I want the top toolbar to change the options.

    
asked by Eric Retamero 17.02.2017 в 20:33
source

1 answer

1

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);
    }
}
    
answered by 17.02.2017 в 20:38