Get context in class without activity

Is there a way in an android application to get the context in android in the non-activity class if the name of the activity class is known?

+62
android android-context
Jul 29 '13 at 7:10
source share
1 answer

If your class is an inactivity class and instantiating it from activiy, you can pass the context instance through the constructor from the following:

class YourNonActivityClass{ // variable to hold context private Context context; //save the context recievied via constructor in a local variable public YourNonActivityClass(Context context){ this.context=context; } } 

You can instantiate this class from an action as follows:

 new YourNonActivityClass(this); 
+101
Jul 29 '13 at 7:15
source share



All Articles