Reputation: 310
I'm writing a class that contains functions of frequent use. Something like this:
public class myLib{
public static String var1="...";
public static Int var2 = 123;
public static void function1() {...}
public static void function2() {...}
}
This lib is intended to be used in android projects, and some of those static functions require access to the application context. How do I get the application context of the application calling these static function?
Upvotes: 0
Views: 465
Reputation: 271
If you are calling static function from Activity then during calling just pass context as parameter.
Otherwise extend your class as Application and create constructor and use context.
Upvotes: 0
Reputation: 1164
Pass Context as a param to methods that need it.
The param can be final @NonNull Context context
Upvotes: 0
Reputation: 2774
You can use the context from function parameter and pass the context whenever you call the function from fragment or activity.
public class myLib{
public static String var1="...";
public static Int var2 = 123;
public static void function1(Context context, ...) {...}
public static void function2() {...}
}
from Activity,
myLib.function1(this,...);
Upvotes: 1