android - Using methods from an activity in another activity -


i have few "main" activities game , have created class include methods going used in various main activities.

one method this:

    public void printmessage(string message, int time)     {        toast toastmessage = toast.maketext(this, "", toast.length_short);        toastmessage.settext(message);        toastmessage.setduration(time);        toastmessage.show();     } 

obviously, method requires class extend activity, i'm guessing problem.

i read using intents , putextra, getextra... overkill in case. know printmessage doesn't justify new class, have more methods , require activity well.

i have mention i'm extremely new android , new java. right i'm used java's way of creating object , method call object. how tried here, works "regular classes", not activity classes.

thank you.

the class doesn't need extend activity, pass context of calling activity printmessage method adding parameter this...

public void printmessage(context context, string message, int time) {     toast toastmessage = toast.maketext(context, "", toast.length_short);     ... } 

edit: in response comment...

from can tell, you've created 'helper' class isn't android specific needs use android elements (such context of activities, sharedpreferences etc). in many cases these things can passed various methods of helper class (without problem) in way demonstrated above.

it work, but, unfortunately many toastmessages.

ok, it's being called repeatedly before previous toast times out. try making 'toastmessage' instance member of helper class...

public class myhelper {      toast toastmessage = null;      public void printmessage(context context, string message, int time) {         if (toastmessage != null)             toastmessage.cancel();         toastmessage = toast.maketext(context, "", toast.length_short);         ...     } } 

unfortunately, getsharedpreferences() need activity, or @ least way i'm calling it.

again, methods of 'helper' class can passed context of activity calls them , can used getting preferences etc.

alternatively, mentioned fvz can extend application class , provide access application-wide shared preferences...

public class myapp extends application {      protected static sharedpreferences userprefs = null;      @override     public void oncreate() {         super.oncreate();         userprefs = getsharedpreferences("myappprefs", mode_private);     } } 

all activities can use (for example)...

myapp.userprefs.putstring(...); 

in case, create helper class static member of application making accessible activities.

is there simpler way use methods similar regular java?

one thing grasp android use 'regular' java...it's android activity class special case. when 1 activity covers another, have no guarantee how long first activity stay alive. in many cases destroyed - reason, can't rely on being able call method in 1 activity another. it's not way android supposed work.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -