Hide MenuItem in some Fragments Hide MenuItem in some Fragments android android

Hide MenuItem in some Fragments


In the fragment where you want to hide the Item

@Overridepublic void onPrepareOptionsMenu(Menu menu) {    MenuItem item=menu.findItem(R.id.action_search);    if(item!=null)       item.setVisible(false);}

and in onCreate() of your fragment

 setHasOptionsMenu(true);


In the Fragment where you don't want to show any menu options, you need setHasOptionsMenu(false); in the onCreate(), like this:

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setHasOptionsMenu(false);}

However, the menu that is being shown that you would like to hide (REFRESH), belongs to MainActivity. That is why it is always shown. Since you want to control the menu at the Fragment level (and not show an Activity options menu), my suggestion is to delete the menu code from the Activity and implement it in your Fragment.

Activitys and Fragments can each have their own separate menus. See this link.


please try this

@Override public void onPrepareOptionsMenu(Menu menu) {    menu.clear();}

and put this on your fragmen's onCreate()

setHasOptionsMenu(true);