关于Fragment和Activity之间onCreateOptionsMenu的问题

泡在网上的日子 / 文 发表于2014-11-20 16:13 次阅读 menu,fragment

Fragment和Activity一样,可以重写onCreateOptionsMenu方法来设定自己的菜单,其实这两个地方使用onCreateOptionsMenu的目的和效果都是完全一样的,但是由于Fragment是从属于activity的,因此第一次使用onCreateOptionsMenu的时候需要注意以下知识点。

一、在Activity和Fragment中onCreateOptionsMenu的实现是有细微差别的

在activity中:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

在Fragment中:

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

两者不同的地方在于

(1)一个有返回值(boolean类型),一个没有返回值。

(2)Fragment中onCreateOptionsMenu的参数多了一个MenuInflater


二、想让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其实是一个对象。

我还可以从上面的分析中得出,Fragment的菜单项会显示在Activity菜单项的后面。


为了解决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="100"
        android:title="@string/theme_color_pick"
        app:showAsAction="never"/>              
</menu>

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="100"
        android:title="@string/show_in_detail"
        app:showAsAction="never"/>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
</menu>

他们有相同的菜单multi_select(主要是id相同)。

这时如果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", 500).show();
            break;      
    }
    return super.onOptionsItemSelected(item);
}

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);
}

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

收藏 赞 (22) 踩 (4)
上一篇:用PopupWindow实现自定义溢出(overflow)菜单
当Action Bar的Action放不下时,系统会将其收集在overflow中。 用hierarchyviewer查看系统自己生成的Overflow,发现它本身就是popupWindow。 所以我们也可以用popUpWindow来写自己的overflow实现更多功能,做出像微信一样的效果。 第一次写,废话有点多,还
下一篇:使用ShareActionProvider分享数据
如果你还不知道关于android分享的基础知识,请先看: Android应用程序间的内容分享机制 一文。 ShareActionProvider其实是ActionProvider的一种。官网的说法是ShareActionProvider让分享变得更容易,不过在我看来ShareActionProvider的真正优势是更简洁漂亮