Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to get an Application Context into a static method in Android

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?

like image 855
Slapout Avatar asked May 07 '10 01:05

Slapout


People also ask

How do you pass a static class context?

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.

How do you get the context of an application class?

Do this: In the Android Manifest file, declare the following. Now everywhere call MyApplication. getAppContext() to get your application context statically.

How do I get application context from activity?

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.

What is the difference between getContext () getApplicationContext () getBaseContext () and this?

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.


2 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.

like image 197
Karan Avatar answered Sep 30 '22 02:09

Karan


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. :)

like image 34
Versa Avatar answered Sep 30 '22 04:09

Versa