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?
-
Possible duplicate of [Static way to get 'Context' on Android?](http://stackoverflow.com/questions/2002288/static-way-to-get-context-on-android) – Robby Cornelissen Jul 26 '16 at 03:42
4 Answers
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.

- 12,724
- 6
- 40
- 33
This should get you access to applicationContext
from anywhere allowing you to get applicationContext
anywhere that can use it; Toast
, getString()
, sharedPreferences
, etc. I have used this to get applicationContext
inside of static methods multiple times.
The Singleton:
package com.domain.packagename;
import android.content.Context;
/**
* Created by Versa on 10.09.15.
*/
public class ApplicationContextSingleton {
private static PrefsContextSingleton mInstance;
private Context context;
public static ApplicationContextSingleton getInstance() {
if (mInstance == null) mInstance = getSync();
return mInstance;
}
private static synchronized ApplicationContextSingleton getSync() {
if (mInstance == null) mInstance = new PrefsContextSingleton();
return mInstance;
}
public void initialize(Context context) {
this.context = context;
}
public Context getApplicationContext() {
return context;
}
}
Initialize the Singleton in your Application
subclass:
package com.domain.packagename;
import android.app.Application;
/**
* Created by Versa on 25.08.15.
*/
public class mApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ApplicationContextSingleton.getInstance().initialize(this);
}
}
If I´m not wrong, this gives you a hook to applicationContext everywhere, call it with ApplicationContextSingleton.getInstance.getApplicationContext();
You shouldn´t need to clear this at any point, as when application closes, this goes with it anyway.
Remember to update AndroidManifest.xml
to use this Application
subclass:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.domain.packagename"
>
<application
android:allowBackup="true"
android:name=".mApplication" <!-- This is the important line -->
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:icon="@drawable/app_icon"
>
Please let me know if you see anything wrong here, thank you. :)

- 605
- 7
- 11
-
1I don't know if this way is a best way to do it. But I think initializing singletons which need a context at entrance of `Application` is a good idea. If not, I must pass a `Context` to `SingletonClass.getInstance(Context)` every time I call it and it's really annoying. – ProtossShuttle Sep 28 '15 at 05:08
-
Yeah, it is a bit annoying, I´ve been using classes like these [gPrefs](https://gist.github.com/kai-kb/0393e6239db3b90a771c) [gString](https://gist.github.com/kai-kb/3da8389513c21a671dbe) to combat that to some degree. – Versa Sep 29 '15 at 10:39
-
This approach also generates a Warning concerning leak and Instant Run issues: "Do not place Android context classes in static fields (static reference to ApplicationContextSingleton which has field context pointing to Context); this is a memory leak (and also breaks Instant Run). A static field will leak contexts. – WilliamK Feb 10 '17 at 01:49
-
3@WilliamK I don't think application context can ever leak, since the entire app gets killed when Application is lost. That it breaks instant run is a bit concerning, and I guess it can't be called future-proof as Application could cease to be a god-object at some point. – Versa Feb 10 '17 at 09:13
-
1@WilliamK Change `Context` to `Application` in the field type to remove this warning. – Thomas Jan 18 '21 at 11:53
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

- 1,428
- 14
- 21
-
6Just be ready to find baseContext null from static contexts that run before the non-static OnCreate method is called. – Melinda Green Oct 19 '11 at 02:01
-
11
-
-
3You have a full explanation of why you cannot make a static reference to the activity context in here: http://goo.gl/rxz5df and here http://goo.gl/1PauC – jpardogo Sep 04 '14 at 17:51
-
-
If you are going to use a static field for storing a context, use the application context, not activity contexts. – sisisisi Nov 26 '18 at 22:39
There's a post in Sane Tricks For InsaneWorld blog with an answer. It says you can replace the Application object with your own subclass and then keep the app context statically there. You can find example code in the post.
The original blog post - http://uquery.blogspot.co.il/2011/08/how-to-get-application-context.html

- 3,397
- 1
- 24
- 25
-
1As of Android Studio 2.2.2, this approach generates a Warning: "Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run). A static field will leak contexts." – WilliamK Feb 10 '17 at 01:44
-
1@WilliamK there is an exception to this rule with the application context. It lives as long as your app lives so it can't leak. Do not put an activity context in a static field though! – JensV Feb 13 '18 at 07:10