1

I'm trying to create a static utility class which will contain simple functions like Messagebox(AlertDialog), EmailSender, etc... and will call these functions on other activites. But as I understand I can not create a static class if it is not in a class. What do you suggest for this kind of utility classes?

sa_
  • 2,854
  • 3
  • 22
  • 29
  • what about using a Singleton? I frequently use this pattern if I want to have statefull shared functionalities among different activities. – elbuild Oct 19 '13 at 14:55
  • Can you give an example please. – sa_ Oct 19 '13 at 15:00
  • What you should do is to create a singleton properly by: http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java – zegnus Oct 19 '13 at 15:35
  • right way to create utility class http://stackoverflow.com/a/31581218/3496570 – Zar E Ahmer Jul 23 '15 at 07:50

1 Answers1

4

Why not make it that way

class Utils{
    public static void makeToast(Context context, String text){
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }
}

In your activity call it this way

Utils.makeToast(this,"hi");
A.S.
  • 4,574
  • 3
  • 26
  • 43