How can I use onCreateOptionsMenu in a Fragment within a tab Activity? (Sherlock ActionBar)

有些话、适合烂在心里 提交于 2019-12-04 08:49:49

问题


I have the following method overridden in my tab Activity and it works fine like that but I want specific option menu's for each fragment. When I put this in my fragment and press the menu button, nothing happens.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.activity_calc, menu);
    return true;
}

回答1:


You need to call setHasOptionsMenu(true) on your fragment before it can participate in the action bar.




回答2:


May I suggest you check out http://code.google.com/p/sherlock-demo/. As BackpackOnHead describes, it uses ViewPager + ActionBar Tabs to let you navigate two different ways between the Fragments in an Activity. In this case, it is the FragmentTabs portion of the API Demos sample app, ported to ActionBarSherlock. The LoaderThrottle Fragment in particular has its own OptionsMenu items.



来源:https://stackoverflow.com/questions/11855581/how-can-i-use-oncreateoptionsmenu-in-a-fragment-within-a-tab-activity-sherlock

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