java - Methods available across an app? -
i'm 100% sure going 1 of newbie questions, here goes...
is there way can write method in 1 activity , able access others?
example: have 6 activites in app, each it's own menu.xml because options available each need different, , have these menus & menuitems set shown:
@override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.calculator_menu, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { //handle item selection switch (item.getitemid()) { case r.id.menuitem_calculator_help: helpdialoggo(); return true; case r.id.menuitem_calculator_settings: //settingsactivitygo(); return true; case r.id.menuitem_calculator_share: sharego(); return true; case android.r.id.home: // app icon in action bar clicked; go home intent uptohome = new intent(this, main.class); uptohome.addflags(intent.flag_activity_clear_top); startactivity(uptohome); return true; default: return super.onoptionsitemselected(item); } } the example of 1 of these methods is:
private void helpdialoggo() { toast.maketext(this, "help", toast.length_long).show(); alertdialog.builder alt_bld = new alertdialog.builder(this); alt_bld.setmessage("sorry, no has been written since application still in development. prerelease version.") .setcancelable(false) .setpositivebutton("cool", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { // action 'yes' button dialog.cancel(); } }) .setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { // action 'no' button dialog.cancel(); } }); alertdialog alert = alt_bld.create(); // title alertdialog alert.settitle("pixel help"); // icon alertdialog alert.seticon(r.drawable.question); alert.show(); } so there way have custom method shared among activities , run when button pressed in each of them, avoid having large amounts of code replicated across app?
and if so, there potholes may hit? (some of menu items going bring dialogs, others take user new activity)
do have similar menuitems in every activity? i.e. same number of items different behaviour? if yes... how creating baseactivity overrides oncreateoptionsmenu , onoptionsitemselected() methods.. (as have given in above example). activities should inherit baseactivity , override menu handling methods. eg. helpdialoggo() go new class.
so baseactivity have oncreateoptionsmenu , onoptionsitemselected() methods. plus menuitem actions (i.e. helpdialoggo() etc) empty methods. inherited classes overide menuitem actions.
if menuitems not similar in each activity, better off creating menu each activity.
edit:
not sure expect more. thought made clear. let me try again.
class baseactivity extends activity.
baseactivity extends activity { // copy oncreateoptionsmenu() , onoptionsitemselected() methods here protected void helpdialoggo() { } // ... other methods } class myactivity1 extends baseactivity.
myactivity1 extends baseactivity { // copy helpdialoggo() code in full here , make // specific changes menu behaviour based on activity. } class myactivity2 extends baseactivity
myactivity2 extends baseactivity { // copy helpdialoggo() code in full here , make // specific changes menu behaviour based on activity. }
Comments
Post a Comment