46515

Action buttons are shown in all fragments

Question:

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 setting setHasOptionsMenu(false).

Anyone knows how to disable action buttons in other fragments or you need to inflate an empty menu in each fragment ?

Thanks in advance

This is my code (in fragment) :

@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); menu.clear(); inflater.inflate(R.menu.main, menu); } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setHasOptionsMenu(true); }

Answer1:

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); }

Recommend

  • Binary XML file line #26: Duplicate id, tag null, or parent id with another fragment
  • List view crashes when scrolling “The application may be doing too much work on its main thread.”
  • Listview with multiple strings
  • Variant from android-autofittextview library : scaling makes strange behaviour
  • Easy way to convert a Bitmap and Png Image to text and vice versa
  • Stacked Bar Chart with percentage composition inside the Bar and total above the Bar in JFreeChart
  • javascript variables, What does var x = a = {} do?
  • Get the last date of each month in a list of dates in Python
  • How secure are apple APNS push notifications?
  • How to define an array of floats in Shader properties?
  • WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER. Wit
  • Loading fixtures in sails tests
  • Activity overlaying. WindowManager.LayoutParams
  • How to render a pixel array most efficiently to a window in c++?
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • Assign variable to the value in HTML
  • How to use carriage return with multiple line?
  • Jackson Parser: ignore deserializing for type mismatch
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Why is an OPTIONS request sent to the server?
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Deleting and Updating values from a cusrsor adapter
  • Possible to stop flickering java tooltip in heavyweight mode?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Perl system calls when running as another user using sudo
  • sending/ receiving email in Java
  • Cannot Parse HTML Data Using Android / JSOUP
  • Acquiring multiple attributes from .xml file in c#
  • JTable with a ScrollPane misbehaving
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?