Show various menu options on toolbar for activity and fragment

Android Studio 1.4 

I have a toolbar that I am inflating in my activity_main.xml . I have a menu called main.xml that is pumping up and there is only 1 icon on it.

When the user clicks to open the fragment. I have another friends.xml menu that has 2 icons.

When I inflate the friends menu in the fragment, it still displays the icon in the main.xml menu.

I thought that inflating a new menu on the toolbar would delete the existing menu.

This is a screenshot of the main.xml menu. A search icon is displayed. enter image description here

This is a screenshot of a snippet since you see that the find icon still exists.

enter image description here activity_main.xml with toolbar enabled

 <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" xmlns:app="http://schemas.android.com/apk/res-auto"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include android:id="@+id/tbMain" layout="@layout/app_bar" android:layout_width="match_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true"/> </LinearLayout> </android.support.v4.widget.DrawerLayout> 

here is my code for creating a menu in MainActivity.java

  private void setupToolBar() { mToolbar = (Toolbar)findViewById(R.id.tbMain); setSupportActionBar(mToolbar); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); } 

And in my fragment I have this, as you can see that I am inflating the friends.xml menu.

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

Thanks so much for any suggestions,

+6
source share
2 answers

I'm not sure you can do this with onCreateOptionsMenu() . I think your best bet would be onPrepareOptionsMenu() .

You can force Android to update the options menu by simply writing getActivity().invalidateOptionsMenu() in the onResume() .

So your onPrepareOptionsMenu() will look like this:

 @Override public void onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); menu.clear(); //remove all items getActivity().getMenuInflater().inflate(R.menu.menu_fragment, menu); } 
+11
source

Save the menu link in a variable.

  private Menu menu; @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); this.menu = menu; return super.onCreateOptionsMenu(menu); } 

When replacing a specified fragment, follow these steps:

 private void hideOption(int id) { menu.findItem(id).setVisible(false); } 

Call hideOption() using the menu id. for ex,

 hideOption(R.id.action_search); 

And vice versa for display. or follow the suggestion #Droidwala

+3
source

All Articles