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?
private static Application context; Adjust your method to take an Application as a parameter, and have your call to that method use getApplication() instead of getApplicationContext() . IOW, your code is reasonably safe — you are using the Application context — but the details of your code is making Lint nervous.
Do this: In the Android Manifest file, declare the following. Now everywhere call MyApplication. getAppContext() to get your application context statically.
You can go for getApplicationContext() if you wanna get context of whole application. If you want to get context of current class you can use getBaseContext() instead.
getApplicationContext() - Returns the context for all activities running in application. getBaseContext() - If you want to access Context from another context within application you can access. getContext() - Returns the context view only current running activity.
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.
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With