views:

297

answers:

2

I'm working on an Android application that has several Activities. In it I have a class with several static methods. I would like to be able to call these methods from the different Activities. I'm using the static methods to load data from an xml file via a XmlResourceParser. To create a XmlResourceParser requires a call on the Application Context. So my question is, what is the best way to get a reference to the Application Context into the static methods? Have each Activity get it and pass it in? Store it somehow in a global variable?

+2  A: 

The better way would be to pass the Activity object as parameter to the static functions.

AFAIK, there is no such method which will give you the application context in the static method.

Karan
Thanks. That's what I ended up doing.
Slapout
A: 

I am not sure this is going to work all the time but it works for me now:

public class myActivity extends ListActivity
{
    public static Context baseContext;

    public void onCreate(Bundle savedInstanceState) 
    {
        baseContext = getBaseContext();
    }

Then you may use the static in your package:

myApplication.baseContext
gjpc