-1

I'm following tutorials on how to put functions that are used frequently in activities all in one place.

For example, a toast message that comes up throughout my project, instead of having the function in each and every activity, just having it called in one place, GlobalFunctions.java.

So, I get it with simple functions, for example, in GlobalFunctions.java :

public class GlobalFunctions {

    public void simpleMessage() {
        System.out.println("simpleMessage text goes here");
    }

}

And the I call it like this from Activity1:

GlobalFunctions simplemessage = new GlobalFunctions();
simplemessage.simpleMessage();

But what about? :

public class GlobalFunctions {

    public void simpleMessage() {
        Toast.makeText(getApplicationContext(), "simpleMessage text goes here", Toast.LENGTH_LONG).show();
    }
}

I've looked at several posts including getApplicationContext() error Android and no matter what I put in the Context part of Toast I get a Cannot resolve method message. Also if there's any good tutorials for Dummies on this subject I'd be grateful.

CHarris
  • 2,693
  • 8
  • 45
  • 71

5 Answers5

1

One solution would be to pass the Context as a parameter from the Activity or Fragment.

And instead of instantiating GlobalFunctions, writing and using static methods can be a better approach.

merterpam
  • 765
  • 6
  • 14
1

Create a Java Utils class:

   public class Utils {

        public static void showToast(Context context, String text) {
              Toast.makeText(context, text, Toast.LENGTH_LONG).show();
        }

    }

    // for example on the Activity code

        Utils.showToast(this, "This is the toast text");
from56
  • 3,976
  • 2
  • 13
  • 23
1

The key is static .

Static values allow you to use static methods variables ..etc in whole project.

You can use following concept:

public static class GlobalFunctions {

public static void simpleMessage(Context context, String message) {
    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}

}

And you have to invoke it like:

GlobalFunctions.simpleMessage(/*YourActivity.this*/ /*or*/ /*getActivity()*/, "toast");
1

try this Create class like this to pass Context and Toast message as parameter like this

public class GlobalFunctions {      

    public static void simpleMessage(Context context,String message) {
        Toast.makeText(Context, message, Toast.LENGTH_LONG).show();
     }
}

call this function like this

  GlobalFunctions.simpleMessage(YourActivity.this,"your Mesaage");
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • `GlobalFunctions.simpleMessage(YourActivity.this,"your Mesaage");` wont work because that's not static class also method, you have to extract it from variable. –  Oct 14 '17 at 13:54
  • @Ibrahim i was just updating my ans – AskNilesh Oct 14 '17 at 13:55
  • I would prefer to keep your old answer, because it was carring different concept which more useful to whole question, now you edited it like all answers in the question which carrieng same concept and way so it's became some useless before. –  Oct 14 '17 at 14:02
  • @Ibrahim didn't get u my friend – AskNilesh Oct 14 '17 at 14:03
1

Keeping context in field beyond activity can be reason of memory leak, but there is some workaround.

You can create Singleton with application or application context and initialize it in onCreate in your custom application class. But you have to remember that you can't use this context to build views - it is not stylized.

Other way is just pass context as argument.

Sorry for missing code, response from phone :)

Karol Kulbaka
  • 1,136
  • 11
  • 21