Unable to override onCreateOptionsMenu in ListFragment

后端 未结 10 1705
太阳男子
太阳男子 2020-12-24 06:56

I created an app that supports both phone and tablet version so i use the android-support-v4.jar library.

My activity extends the ListFragment and I tried to overri

10条回答
  •  遥遥无期
    2020-12-24 07:12

    I had the same problem and this what I did to use onCreateOptionsMenu of Fragment. Override the onCreate method of the Fragment and make sure that you use setHasOptionsMenu method with parameter value "true" to let the system know Fragment will use OptionsMenu.

    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setHasOptionsMenu(true);
    }
    

    Then override onCreateOptionsMenu to inflate your menu xml file (here in this example I inflated fragmentmenu.xml

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

提交回复
热议问题