How to make different menu options in different Fragment?

流过昼夜 提交于 2020-02-27 06:36:39

问题


I want to have completely different menu options in different fragment.I followed this post.But my fragment menu is adding with the activity menu.But i don't want to have activity menus in some of my fragments.
In SlidingDrawerActivity:

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

In my fragment:

public Friends_Status_Comment_Fragment(){
        setHasOptionsMenu(true);
    }
  @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_add_comment,menu);
        super.onCreateOptionsMenu(menu, inflater);

    }

The Activities items are adding with the menu of fragment.How to stop it???


回答1:


I'm not sure if I underestand your problem - in your fragment you should clear menu and create new one - and don't call super :) something like this:

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


来源:https://stackoverflow.com/questions/28096553/how-to-make-different-menu-options-in-different-fragment

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