menu inflating calls multiple times at fragment's onCreateOptionsMenu

前端 未结 3 556
逝去的感伤
逝去的感伤 2021-02-13 01:54

I use Fragments and when I switch to nested Fragment, which implements public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) my menu inflates quantity o

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-13 02:41

    Just check the count of menu items. Meaning menu.size()==0 ,no menu items are present,then inflate with layout menu,else don't inflate at all.

     @Override
     public void   onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
          if (menu.size() == 0)
          inflater.inflate(R.menu.call_menu, menu);
          super.onCreateOptionsMenu(menu, inflater);
    
     }
    

提交回复
热议问题