Android : Fragment has action bar item of other fragment

on Thursday, August 21, 2014


I have a Activity which displays two fragments. If replace the first fragment which is shown by default, the second fragment holds additionally to it's own action bar items the action bar items of the first fragment in its action bar and I don't know why.


Activity menu xml



<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
</menu>


Activity onCreateOptionsMenu



@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_fragment, menu);
return true;
}
private void showDetail(long id) {
// Replace the fragment
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer,
DetailFragment.newInstance(id))
.addToBackStack(null)
.commit();
}


list fragment menu xml



<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_item_new"
android:icon="@android:drawable/ic_menu_add"
android:orderInCategory="2"
android:showAsAction="always"
android:title="New"/>
</menu>


list fragment onCreateOptionsMenu



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** ... */
setHasOptionsMenu(true);
/** ... */
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_list, menu);
}


detail fragment menu xml



<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_item_edit"
android:icon="@android:drawable/ic_menu_edit"
android:orderInCategory="1"
android:showAsAction="always"
android:title="@string/menu_strings_edit" />
</menu>


list fragment onCreateOptionsMenu



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** ... */
setHasOptionsMenu(true);
/** ... */
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_list_detail, menu);
}


Why does the second (detail) fragment contains action bar items from the first fragment? I tested it in a new project and found that this not default behavior but i don't find the mistake here.


0 comments:

Post a Comment