John Bravado
John Bravado

Reputation: 137

Public Methods for all activities

I have a class called myConstants and in it i list all my constants so when i need them I just reference MyConstants.MYCONSTANT. However, i would like to implement something like this for methods. i am repeating a lot of code, for instance, i have a formatCalendarString(Calendar c) method in 3 activities. seems redundant and unecessary. but i cant make them static because i get static calling non-static errors and the only other way i can think is to make a MyConstant object then call public functions off that object, like this...

MyConstants myConstants = new MyConstants();
myConstants.formatCalendarString(Calendar.getInstance());

is there some way i can just call the formatCalendarString() inside MyConstants class without generating an object?

Upvotes: 0

Views: 528

Answers (5)

Krupal Shah
Krupal Shah

Reputation: 9187

You can use singleton pattern to cache instances. Keeping methods in something like parent activity does not make any sense (as primary role of activity is user interaction).

Example:

public class MyConstants {
    private static MyConstants ourInstance;

    private MyConstants() {
        //private constructor to limit direct instantiation
    }

    public synchronized static MyConstants getInstance() {
        //if null then only create instance
        if (ourInstance ==null) {
            ourInstance = new MyConstants();
        }
        //otherwise return cached instance
        return ourInstance;
    }

}

You just need a private constructor and public static method that would only generate instance if it is null.

Then, call MyConstants.getInstance().whateverMethod(). It will create only single instance.

However when using singleton, please keep memory leaks in mind. Do not pass activity context directly inside singletons.

Upvotes: 3

Pier Giorgio Misley
Pier Giorgio Misley

Reputation: 5351

You can use the Static keyword.

Static methods can be referenced from outside without istantiating the new object.

Just create a class:

public class MyClassContainingMethods{

  public static String MyStaticMethod(){
    return "I am static!";
  }
}

Now call it like

String res = MyClassContainingStaticMethods.MyStaticMethod();

Hope this helps

NOTE

You CAN call non-static from static by doing something like this:

  public static void First_function(Context context)
  {
    SMS sms = new SMS();
    sms.Second_function(context);
  }

  public void Second_function(Context context)
  {
    Toast.makeText(context,"Hello",1).show(); // This i anable to display and cause crash
  }

Example taken from here, you will obiouvsly have to fit it into your needs

Upvotes: -1

V. Mäntylä
V. Mäntylä

Reputation: 21

I agree with Pier Giorgio Misley. It's also good to add a private constructor, because you don't obviously want to instantiate an object.

Upvotes: 0

user2898051
user2898051

Reputation: 109

Can't you just use a parent class? That way you can just inherit the methods and manage in one source. Then you don't have to use static functions then.

Edit: Like Tomasz Czura said, just extend the Class.

public class ParentClass {

public void commonMethod(){ } }

public class OtherClass extends ParentClass{ }

Upvotes: -1

Tomasz Czura
Tomasz Czura

Reputation: 2434

If you want to have all methods in activities, you can put then in abstract class BaseActivity, which extends Activity, and then make your activities extends BaseActivity. However, if these methods doesn't correspond to something about activity, I suggest some Singleton or Util class

Upvotes: 0

Related Questions