Java - Duplicate methods in classes and how to call them from another class -


i learning , loving java , android have long way go. best practice question, think. in android activity have 6 classes. several of them calling methods have duplicated class. seems redundant duplicate methods when call them class. think easier maintain them in 1 class. (main activity, maybe?) question is: best practice calling same method more 1 class? example, classes are:

main activity gameselector game1home game1

i have few methods same in every class. lets call them getprefs() , setprefs(). not passing them or out of them. class should go in, , how call them class?

edit - helpful answers have functioning configurations class , other 6 classes cleaner! easy maintain , learn few great pointers while making it. i'm posting finished class here in case may else. can call methods other classes this:

configurations.getprefs(this); 

and refer static variables you've defined global in configurations file this:

configurations.buttonclicked.start(); 

configurations.java:

public class configurations extends activity { static mediaplayer buttonclicked; static mediaplayer instructionsaudio; static mediaplayer messageaudio; static mediaplayer correctnum_sound; static mediaplayer incnuma_sound; static mediaplayer incnumb_sound; static string storechildsname; static string storerequestedrange; static string storevoicechoice; static intent i;   public static void setupprefs(final activity a) {     imagebutton settingsclicked = ((imagebutton) a.findviewbyid(r.id.prefbutton));     settingsclicked.setonclicklistener(new onclicklistener() {              public void onclick(view v) {                 imageview settingsclicked = ((imageview) a.findviewbyid(r.id.prefbutton));                     settingsclicked.setimageresource(r.drawable.settings_button_clicked);                 buttonclicked = mediaplayer.create(a, r.raw.click);                     buttonclicked.start();                      intent settingsactivity = new intent(a.getbasecontext(),                                     preferences.class);                     a.startactivity(settingsactivity);             }     }); } public static void getprefs(final activity a) {     // xml/preferences.xml preferences     sharedpreferences prefs = preferencemanager                     .getdefaultsharedpreferences(a.getbasecontext());     storechildsname = prefs.getstring("edittextpref",                     "friend");     storerequestedrange = prefs.getstring("listpref", "3");     storevoicechoice = prefs.getstring("voices", "1"); }  public static void setupmusictoggle(final activity a) {     i=new intent(a, mymusicservice.class);        final togglebutton togglebutton =(togglebutton)a.findviewbyid(r.id.music_toggle);       togglebutton.setonclicklistener(new onclicklistener() {             public void onclick(view v) {                  // perform action on clicks                   if (togglebutton.ischecked()) {                      toast.maketext(a, "music on.", toast.length_short).show();                     a.startservice(i);                 } else {                      a.stopservice(i);                   toast.maketext(a, "music off.", toast.length_short).show();                        }    }});  }  public static void returnhome(view view, activity a) {     imageview homeclicked = (imageview) a.findviewbyid(r.id.home);     homeclicked.setimageresource(r.drawable.homebuttonclicked);     buttonclicked = mediaplayer.create(a, r.raw.click);     buttonclicked.start();     intent intent = new intent(a, gameselector.class);     a.startactivity(intent); }  public static void releasemp(activity a) {     if (buttonclicked != null) {         buttonclicked.stop();         buttonclicked.release();}         if (instructionsaudio != null) {             instructionsaudio.stop();             instructionsaudio.release();}         if (messageaudio != null) {             messageaudio.stop();             messageaudio.release();         }         if (correctnum_sound != null){             correctnum_sound.stop();             correctnum_sound.release();         }         if (incnuma_sound != null) {             incnumb_sound.stop();             incnuma_sound.release();         }         if (incnumb_sound !=null) {             incnumb_sound.stop();             incnumb_sound.release();         } }  public static boolean ismyservicerunning(activity a) {     activitymanager manager = (activitymanager) a.getsystemservice(context.activity_service);     (runningserviceinfo service : manager.getrunningservices(integer.max_value)) {         if (mymusicservice.class.getname().equals(service.service.getclassname())) {             return true;         }     }     return false; } 

}

hope helps as it's helped me!

ps - if see room improvement in code please share! have lot learn =)

there several ways share code - can by

  1. defining "helper" class, , adding static methods it,
  2. defining base class (often abstract base class) , adding methods it,
  3. defining non-static class, , embedding instance of class in each of classes need share code; classes share reference common instance.

it hard approach more appropriate, method names appears planning , set preferences. in situations that, #1 or #3 shared instance appropriate.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -