Question

I was using activity, then i want to change my code to use viewpager with fragment

what i was using activity, i was able to show the menu. however, when i changed my code to fragment, i am not able to show the menu.

code

This code in the fragment

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

enter image description here

enter image description here

Was it helpful?

Solution

You need to call setHasOptionsMenu(true) for the Fragment.

Eg using the Fragment's onCreate() method

public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setHasOptionsMenu(true);
}

You also do not need the call to super.onCreateOptionsMenu(menu, inflater);, the superclass method is empty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top