33

The following schema has been touted as the way to get application context from anywhere within my android app. But sometimes doing MyApp.getContext() returns null. I tried changing the schema by removing static from getContext() so that I would do MyApp.getInstance().getContext(). It still returns null. How do I fix this? How do I get my application's context from anywhere within my app?

public class MyApp extends Application {
    private static MyApp instance;

    public static MyApp getInstance() {
        return instance;
    }

    public static Context getContext() {
        return instance.getApplicationContext();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }
}
learner
  • 11,490
  • 26
  • 97
  • 169

7 Answers7

55

Create in onCreate() an instance of getApplicationContext() (mContext) then call MyApp.getContext() from everywhere in your app and you will get your application context statically.

public class MyApp extends Application {
    private static Context mContext;

    public static Context getContext() {
        return mContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();    
    }
}

Remember to declare into your AndroidManifest.xml

<application android:name="com.mypackage.mypackage.MyApp">
...
...
...
</application>
Sabrina
  • 2,531
  • 1
  • 32
  • 30
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 12
    The link solved my problem. I was missing ``. Thanks. – learner Feb 25 '14 at 05:44
  • `return instance.getApplicationContext()` works fine, no need to use `return mContext`. Particularly when calling `instance.getApplicationContext()` from classes outside application in some cases like database, it is preferred to pass instance as context, and then call Application context. – Nagaraju Gajula Oct 28 '16 at 06:13
  • 2
    i have this warning "Do not place Android context classes in static fields; this is a memory leak ", how can i resolve this? – Hamid Zandi Jul 21 '17 at 17:43
  • 1
    @juzamn in services sometime Myapp.getContext through null pointer exception – baldraider Oct 14 '17 at 11:55
  • @do01 it's a application object. I don't think any leak willl affect application memory management. – Shubham AgaRwal Mar 29 '19 at 09:14
12

Create a static instance of the Context in your OnCreate and keep it till you want to get it from a getter method getContext()

From the Application class:

public class MyApp extends Application {

private static Context sContext;
@Override
public void onCreate() {
    sContext = getApplicationContext();
    super.onCreate();
}

public static Context getContext() {
    return sContext;
}
}

Declare it in your Manifest:

<application android:name="com.package.name.MyApp">
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
2

Use the following way to get the Application context.

public class MyApp extends Application {
    private static MyApp mAppInstance=null;
    public static Context appContext;

    public static MyApp getInstance() {
        return mAppInstance;
    }

    public static MyApp get() {
        return get(appContext);
    }

    public static MyApp get(Context context) {
        return (MyApp) context.getApplicationContext();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mAppInstance=this;
        appContext=getApplicationContext();
    }
}

add the the application name inside the Manifest file

<application android:name="packagename.MyApp"/>

to get the context use MyApp.getInstance().getApplicationContext()

Sabrina
  • 2,531
  • 1
  • 32
  • 30
1

instance is never initialized and so has a default value of null. This means that instance.getContext() will throw a NullPointerException. To fix this, you need to initialize the instance variable.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

Currently, you have not initialized instance and by default it's value would now be set to null. You need to assign it a value before you can use it.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
0

Another root cause, is due to the buggy backup process. Please refer to

Why backup related process might cause Application's onCreate is not executed?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
0

For get an application context the module should use the application plugin.

build.gradle (:module)

apply plugin: 'com.android.application'

The library plugin has not an app context.

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62