Android Get Application Context From Anywhere Example

When your android application grows, there are a lot of util classes. And sometimes you need to use android application context in those classes to do something like create a Toast message or create a Snackbar popup. And if you pass the application context object as an input parameter to every util class, you will find that it is impossible and complex. This article will tell you how to get the android application context object in any util class when you need it.

As you can see in the AndroidManifest.xml file, each android app will declare a unique application XML element in it like below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dev2qa.example">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name">
        
        <activity....>
           ......
        </activity>
    </application>

</manifest>

So the best method to resolve this issue is to define a custom application object ( extends android.app.Application ) and use it in your AndroidManifest.xml file like below.

1. Create The Global Application Sub Class.

GlobalApplication.java

package com.dev2qa.example.application;

import android.app.Application;
import android.content.Context;

public class GlobalApplication extends Application {

    private static Context appContext;

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

        /* If you has other classes that need context object to initialize when application is created,
         you can use the appContext here to process. */
    }

    public static Context getAppContext() {
        return appContext;
    }
}

2. Use Above Custom Application In The Android Manifest Xml File.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.dev2qa.example"> 
      <application
          android:name="com.dev2qa.example.application.GlobalApplication"
          android:allowBackup="true" 
          android:icon="@mipmap/ic_launcher" 
          android:label="@string/app_name"> 
          <activity....>
            ......
          </activity>
     </application> 
</manifest>

3. How To Get Application Context Anywhere In Java Class.

Then you can use the below code to get this android app context object anywhere in your java util class and use it when you need it.

Context context = GlobalApplication.getAppContext();
Toast.make(context, "Hello global context. ", Toast.LENGTH_LONG).show();

12 thoughts on “Android Get Application Context From Anywhere Example”

    1. You can not use the “this” keyword, it is because the GlobalApplication object is usually used as a singleton object.

  1. Rarely do I ever reply. I have wasted so much time looking for a solution to this nonsense problem of Android, that I must thank you.

  2. Brilliant! Thanks so much – after searching for a simple solution for this, yours was the only one that makes sense and it actually works. I am tempted to put your solution on stackoverflow, but it will probably get flagged as a duplicate of some non-answer :-(.

  3. omg thank you! Can yo explain this part please?

    /* If you has other classes that need context object to initialize when application is created, you can use the appContext here to process. */

    1. That means if you want to use the appContext object to initialize other class object, you can use the application context created here by below code.

      appContext = getApplicationContext();

      Because this object is a static object, so it can be used globally.

      1. No, it can be used globally, because it’s public. The static modifier only assures, that the object is a “class” Parameter. That means it is the same for every different object of the “GlobalApplication” class.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.