40

In Android, is there any way to get the context of the application of a static way? For example to retrieving it from a background thread.

Thanks

Caroline
  • 611
  • 1
  • 6
  • 11
  • 1
    In general you should pass a Context reference via an argument if you call a method that needs it *(but don't store the reference, it can lead to memory leaks)*. You can also use the Application-Object for this in some circumstances, see [this question](http://stackoverflow.com/questions/987072/using-application-context-everywhere). But keep the limits as described in the answer in mind. –  Feb 25 '12 at 16:16

2 Answers2

95

The easiest (and correct) way is:

Define a new class

public class MyApp extends Application {
    private static MyApp instance;

    public static MyApp getInstance() {
        return instance;
    }

    public static Context getContext(){
        return instance;
        // or return instance.getApplicationContext();
    }
    
    @Override
    public void onCreate() {
        instance = this;
        super.onCreate();
    }
}

Then in your manifest you need to add this class to the "Name" field at the "Application" tab. Or edit the xml and put

<application
    android:name="com.example.app.MyApp"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    .......
    <activity
        ......

and then from anywhere you can call

MyApp.getContext();
starball
  • 20,030
  • 7
  • 43
  • 238
Addev
  • 31,819
  • 51
  • 183
  • 302
  • 4
    This will work but Just be careful as with using any singleton that you don't abuse it.Read the answer to [this question](http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context/7298955#7298955) explaining why the ApplicationContext is rarely (though sometimes) the right Context to use. Best Way: Unless you simply expose a public method that takes a Context as an argument inside of your classes that require Context (and pass it in from your Activity, etc), this is the way to do it. Exp: public void abc(Context c,int a); and call it from any activity. – Macrosoft-Dev Oct 01 '13 at 08:07
27

Basically you have 2 types of Context - the Activity Context and the Application Context.

It's not necessary to use only one Context for everything. There's problems if you use one Context in every case you need Context. It's best to follow something like this - use Activity Context inside the Activity and the Application Context when passing a context beyond the scope of an Activity, which will help you to avoid memory leaks.

If you read this article you can see the difference between to the two Context.

The Application context will live as long as your application is alive and does not depend on the activities life cycle. If you plan on keeping long-lived objects that need a context, remember the application object.

Instead the Activity Context is associated with the activity and could be destroyed as many times as the activity is destroyed - changing screen orientation, back button etc.

hovanessyan
  • 30,580
  • 6
  • 55
  • 83