Skip to content Skip to sidebar Skip to footer

Action Buttons Are Shown In All Fragments

I have included a menu in my fragment. But when going to other fragments(not overriding menu) in my viewpager the actions buttons remains the same (with all the actions) even when

Solution 1:

You need to call setHasOptionsMenu from the Fragment onCreateView , to tell the Activity that the Fragment has Option Menu. So that the onCreateOptionsMenu will be called.

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View row =  inflater.inflate(R.layout.fragment_main, container, false);

    setHasOptionsMenu(true);
    return row;
}

And, In onCreateOptionsMenu , just clear the menu items

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    super.onCreateOptionsMenu(menu, inflater);
}

Post a Comment for "Action Buttons Are Shown In All Fragments"