Android Development Tour-fragment and activity Oncreateoptionsmenu links

Source: Internet
Author: User

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:

  1. @Override
  2. Public Boolean oncreateoptionsmenu(menu menu) {
  3. Getmenuinflater(). Inflate(R. Menu. Main, menu);
  4.   Return Super. Oncreateoptionsmenu(menu);
  5. }

In the Fragment:

  1. @Override
  2. Public void oncreateoptionsmenu(menu menu, menuinflater inflater) {
  3. Inflater. Inflate(R. Menu. Pictrue_list, menu);
  4.   Super. Oncreateoptionsmenu(menu,inflater);
  5. }

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中调用

  1. @Override
  2. Public void onCreate(Bundle savedinstancestate) {
  3.   Super. OnCreate(savedinstancestate);
  4. Mbucketid = getarguments(). GetInt(Images. Media. bucket_id);
  5. Mcallback = new modecallback();
  6. Sethasoptionsmenu(true);
  7. }

三、如果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中的菜单资源:

  1. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  2. Xmlns:app="Http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="Http://schemas.android.com/tools"
  4. Tools:context="com.example.acctionbaractivitydemo.MainActivity" >
  5. <item
  6. android:id="@+id/take_pic"
  7. android:title=""
  8. Android:icon="@drawable/ic_action_camera"
  9. app:showasaction="Withtext|ifroom"/>
  10. <item
  11. android:id="@+id/multi_select"
  12. android:title="@string/multi_select"
  13. app:showasaction="Withtext|ifroom"/>
  14. <item
  15. android:id="@+id/theme_color_pick"
  16. android:orderincategory="+"
  17. android:title="@string/theme_color_pick"
  18. app:showasaction="Never"/>
  19. </menu>

In fragment:

  1. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  2. Xmlns:app="Http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="Http://schemas.android.com/tools"
  4. Tools:context="com.example.acctionbaractivitydemo.MainActivity" >
  5. <item
  6. android:id="@+id/multi_select"
  7. android:title="@string/multi_select"
  8. app:showasaction="Withtext|ifroom"/>
  9. <item
  10. android:id="@+id/show_in_detail"
  11. android:orderincategory="+"
  12. android:title="@string/show_in_detail"
  13. app:showasaction="Never"/>
  14. </menu>

They have the same menu multi_select (mostly the same ID).

In this case, if the activity

  1. @Override
  2. Public Boolean onoptionsitemselected(MenuItem item) {
  3. Switch(item. Getitemid()) {
  4. case R. ID. Take_pic:
  5. Capturepicture();
  6.   Break;
  7.   Case R. ID. Multi_select:
  8.   Toast. Maketext(mainactivity. This, "ss", ( ). Show();
  9.   Break;
  10. }
  11. return Super. onoptionsitemselected(item);
  12. }

In fragment:

  1. @Override
  2. Public Boolean onoptionsitemselected(MenuItem item) {
  3. Switch(item. Getitemid()) {
  4. case R. ID. Multi_select:
  5. Mgridview. setitemchecked(0,true);
  6. Mgridview. Clearchoices();
  7. Mcallback. Updateseletedcount();
  8. break;
  9. case R. ID. Show_in_detail:
  10. break;
  11. }
  12. return Super. onoptionsitemselected(item);
  13. }

So两者的case R.id.multi_select代码块都会执行。

Android Development Tour-fragment and activity Oncreateoptionsmenu links

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.