android - Implement options menu in Fragment Activity -
i have 2 fragment activities , 1 fragment. here's how app logic looks like:
fragmentactivity ==> fragment b ==> fragmentactivity c.
fragmentactivity parent activity of fragment b , has options menu shows correctly.fragment b contains listview. when list item clicked on fragment b,its details displayed in fragmentactivity c.now want display options menu inside c in actionbar.however, menu displayed after menu button clicked.i want actionbar action.
here code activity c:
public class latestdetailsfragment extends fragmentactivity implements onitemclicklistener { public latestdetailsfragment() { photoimages = new imageitem(); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.fragment_latestdetails); gallery.setonitemclicklistener(this); // sethasoptionsmenu(true); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.details_fragment_menu, menu); return super.oncreateoptionsmenu(menu); } /* * * called when invalidateoptionsmenu() triggered */ @override public boolean onprepareoptionsmenu(menu menu) { return super.onprepareoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // handle item selection switch (item.getitemid()) { case r.id.contact: contactownerfragment contactfragment = new contactownerfragment(); bundle bundle = new bundle(); bundle.putstring(key_phone, phone); contactfragment.setarguments(bundle); contactfragment.show(getsupportfragmentmanager(), "contact"); return true; default: return super.onoptionsitemselected(item); } }
and menu:
<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="be.hcpl.android.example.mainactivity" > <item android:id="@+id/contact" android:icon="@drawable/ic_action_call" android:orderincategory="100" android:title="@string/contact" app:showasaction="ifroom|withtext"> </item>
i cannot use sethasoptionsmenu(true);
on activity because raises error,i don't know why. want display icon on actionbar.
you need use menu.clear()
before inflating menus.
@override public boolean oncreateoptionsmenu(menu menu) { menu.clear(); getmenuinflater().inflate(r.menu.details_fragment_menu, menu); return super.oncreateoptionsmenu(menu); }
from documents
public abstract void clear () remove existing items menu, leaving empty if had been created.
Comments
Post a Comment