Show different menu options on Toolbar for activity and fragment

那年仲夏 提交于 2019-12-21 09:27:24

问题


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

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

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,


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/32967874/show-different-menu-options-on-toolbar-for-activity-and-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!