android - Displaying menu in all the activities without overriding onCreateOptionsMenu and onOptionsItemSelected each time -
while working application, had come come across situation supposed use menu has displayed in entire application when user click menu button.
so used following code in default activity realized menu displaying in activity not in all.
@override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.del_boy_menu, menu); //below comented code changung dynamically // menuitem bedmenuitem = menu.finditem(r.id.home); // bedmenuitem.settitle("title changed"); // system.out.println("oncreate executed"); return true; } @override public boolean onoptionsitemselected(menuitem item) { // todo auto-generated method stub system.out.println("onoptionselected executed"); switch (item.getitemid()) { case r.id.home: // single menu item selected // ex: launching new activity/screen or show alert message toast.maketext(maindeliveryboyactivity.this, "home selected", toast.length_short).show(); // menuhomeactivity startactivity(new intent(context,menuhomeactivity.class)); return true; case r.id.delivered1: toast.maketext(maindeliveryboyactivity.this, "delivered selected", toast.length_short).show(); return true; case r.id.cancelled: toast.maketext(maindeliveryboyactivity.this, "cancelled selected", toast.length_short).show(); return true; case r.id.active: toast.maketext(maindeliveryboyactivity.this, "active selected", toast.length_short).show(); return true; default: return super.onoptionsitemselected(item); } }
so question should copy , paste above code in activities? or there way can skip this?
create 1 global activity called baseactivity
, make of activities extend it.
public class baseactivity extends appcompatactivity{ public void oncreate(bundle icreate){ ... } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.del_boy_menu, menu); .... return true; } @override public boolean onoptionsitemselected(menuitem item) { .... } }
and other activities should extend baseactivity, won't need write code inflate menu everytime.
public class activity1 extends baseactivity{ .... }
Comments
Post a Comment