Home » Android » android – Show different menu options on Toolbar for activity and fragment

android – Show different menu options on Toolbar for activity and fragment

Posted by: admin June 15, 2020 Leave a comment

Questions:
Android Studio 1.4

I have a Toolbar that I inflate in my activity_main.xml.
I have a menu called main.xml that gets inflated that has just 1 icon to display on it.

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

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

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

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

This is the screenshot of the fragment as you can see the find icon is still there.

enter image description here
activity_main.xml with toolbar included

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

Many thanks for any suggestions,

How to&Answers:

I’m not sure if you can do this with onCreateOptionsMenu(). I think your better bet will be onPrepareOptionsMenu().

You can force Android to refresh the options menu by simply writing getActivity().invalidateOptionsMenu() in Fragment’s onResume().

So your onPrepareOptionsMenu() will look like:

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

Answer:

Store the menu reference 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 to mentioned fragment do the following.

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

Call hideOption() with menu id. for ex,

hideOption(R.id.action_search);

And vice versa for showing.or follow suggestion of #Droidwala