Fragment and activity, you can rewrite the Oncreateoptionsmenu method to set their own menu, in fact, these two places use oncreateoptionsmenu the purpose and effect are exactly the same, But since fragment is an activity, the first time you use Oncreateoptionsmenu, you need to be aware of the following points of knowledge.
first, the implementation of Oncreateoptionsmenu in activity and fragment is slightly different :
In the activity:
- @Override
- Public Boolean oncreateoptionsmenu(menu menu) {
- Getmenuinflater(). Inflate(R. Menu. Main, menu);
- Return Super. Oncreateoptionsmenu(menu);
- }
In the Fragment:
- @Override
- Public void oncreateoptionsmenu(menu menu, menuinflater inflater) {
- Inflater. Inflate(R. Menu. Pictrue_list, menu);
- Super. Oncreateoptionsmenu(menu,inflater);
- }
The difference between the two is that
(1) One has a return value (Boolean type), and one has no return value.
(2) The parameters of Oncreateoptionsmenu in fragment are one moreMenuInflater
二、想让Fragment
中的onCreateOptionsMenu生效必须先调用setHasOptionsMenu
方法
一般我们是在nCreate
中调用
- @Override
- Public void onCreate(Bundle savedinstancestate) {
- Super. OnCreate(savedinstancestate);
- Mbucketid = getarguments(). GetInt(Images. Media. bucket_id);
- Mcallback = new modecallback();
- Sethasoptionsmenu(true);
- }
三、如果Fragment和Activity都同时inflate
了一个menu资源文件,那么menu资源
所包含的菜单会出现两次
为什么呢,因为inflater.inflate(R.menu.pictrue_list, menu)
方法的作用其实就是将第一个参数中包括的菜单项追加到menu中。一开始,在activity中menu是空的,当调用了
getMenuInflater().inflate(R.menu.main, menu)
menu中便有了菜单项,而在执行到Fragment的(Menu menu, MenuInflater inflater)
时,activity的menu就传递下来,作为第一个参数。activity和Fragment中的menu其实是一个对象。
I can also conclude from the above analysis that the menu items of fragment are displayed behind the Activity menu item.
为了解决menu资源
所包含的菜单会出现两次
这个问题,一般我们让Activity和Fragment inflate两个不同的菜单(就如上面的例子),Fragment会继承Activity的所有菜单。
四、如果在Fragment和Activity中有相同的菜单元素,并且activity和fragment都对此菜单有响应的话,那么将执行两次响应事件。
除此之外,该菜单元素会显示两次。
举例说明:
activity中的菜单资源:
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- Xmlns:app="Http://schemas.android.com/apk/res-auto"
- xmlns:tools="Http://schemas.android.com/tools"
- Tools:context="com.example.acctionbaractivitydemo.MainActivity" >
- <item
- android:id="@+id/take_pic"
- android:title=""
- Android:icon="@drawable/ic_action_camera"
- app:showasaction="Withtext|ifroom"/>
- <item
- android:id="@+id/multi_select"
- android:title="@string/multi_select"
- app:showasaction="Withtext|ifroom"/>
- <item
- android:id="@+id/theme_color_pick"
- android:orderincategory="+"
- android:title="@string/theme_color_pick"
- app:showasaction="Never"/>
- </menu>
In fragment:
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- Xmlns:app="Http://schemas.android.com/apk/res-auto"
- xmlns:tools="Http://schemas.android.com/tools"
- Tools:context="com.example.acctionbaractivitydemo.MainActivity" >
- <item
- android:id="@+id/multi_select"
- android:title="@string/multi_select"
- app:showasaction="Withtext|ifroom"/>
- <item
- android:id="@+id/show_in_detail"
- android:orderincategory="+"
- android:title="@string/show_in_detail"
- app:showasaction="Never"/>
- </menu>
They have the same menu multi_select
(mostly the same ID).
In this case, if the activity
- @Override
- Public Boolean onoptionsitemselected(MenuItem item) {
- Switch(item. Getitemid()) {
- case R. ID. Take_pic:
- Capturepicture();
- Break;
- Case R. ID. Multi_select:
- Toast. Maketext(mainactivity. This, "ss", ( ). Show();
- Break;
- }
- return Super. onoptionsitemselected(item);
- }
In fragment:
- @Override
- Public Boolean onoptionsitemselected(MenuItem item) {
- Switch(item. Getitemid()) {
- case R. ID. Multi_select:
- Mgridview. setitemchecked(0,true);
- Mgridview. Clearchoices();
- Mcallback. Updateseletedcount();
- break;
- case R. ID. Show_in_detail:
- break;
- }
- return Super. onoptionsitemselected(item);
- }
So两者的case
R.id.multi_select代码块都会执行。
Android Development Tour-fragment and activity Oncreateoptionsmenu links