Use Application Context Variable Globally Throughout Application in Android

We just need to set the value in this variable and get the value in any activity, even in service or broadcast receiver in the application context.

Note: Every application has a context, and Android guarantees that application context will exist as an instance across the application.

So we just need to specify our global class in the application tag of the Android manifest file of the application.

Android will create an instance of that global class and make it available for the entire application context.

We can get the object of that global class using following method:

    Context.getApplicationContext()

Procedure to understand

Steps to understand

In MyGlobalClass.java file creates get and set methods using a public variable.

For instance:

  1. public class MyGlobalClass extends Application {  
  2.     public String myString;   
  3.       
  4.     @Override  
  5.     public void onCreate() {  
  6.         super.onCreate();  
  7.     }  
  8.       
  9.     public String getMyString() {  
  10.         return myString;     
  11.     }  
  12.       
  13.     public void setMyString(String myString1) {      
  14.        myString = myString1;  
  15.     }  
  16. }   
Note:
  1. Context: Is an interface to provide global information about an application environment.

  2. getApplication() is only available on the Activity class and in the Service class, whereas getApplicationContext() is declared in the Context class.

Source Code

MyGlobalClass.java:

  1. package com.example.globalclassdemo;  
  2.   
  3. import android.app.Application;  
  4.   
  5. public class MyGlobalClass extends Application {  
  6.     public String myString;   
  7.       
  8.     @Override  
  9.     public void onCreate() {  
  10.         super.onCreate();  
  11.     }  
  12.       
  13.     public String getMyString() {  
  14.         return myString;     
  15.     }  
  16.       
  17.     public void setMyString(String myString1) {      
  18.        myString = myString1;  
  19.     }  
  20. }  
MainActivity.java
  1. package com.example.globalclassdemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9.   
  10. public class MainActivity extends Activity {  
  11.       
  12.     Button btn;  
  13.     public Context _context;  
  14.       
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         _context = getApplicationContext();  
  20.       
  21.         btn = (Button)findViewById(R.id.button1);  
  22.           
  23.         MyGlobalClass globalVariable = (MyGlobalClass) getApplicationContext();  
  24.         globalVariable.setMyString("AN THE RAW ID");  
  25.       
  26.           
  27.         btn.setOnClickListener(new View.OnClickListener() {  
  28.               
  29.             @Override  
  30.             public void onClick(View arg0) {  
  31.                 // TODO Auto-generated method stub  
  32.                   
  33.                 Intent i=new Intent(MainActivity.this, SecondActivity.class);  
  34.                 startActivity(i);  
  35.                   
  36.             }  
  37.         });  
  38.     }  
  39.     @Override  
  40.     public void onBackPressed() {  
  41.         // do something on back.  
  42.         finish();  
  43.         return;  
  44.     }  
  45.       
  46. }  
SecondActivity.java
  1. package com.example.globalclassdemo;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.widget.Toast;  
  5.   
  6. public class SecondActivity extends Activity {  
  7.       
  8.     public String myGetString = null;   
  9.       
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.second);  
  14.         MyGlobalClass globalVariable = (MyGlobalClass) getApplicationContext();  
  15.         myGetString = globalVariable.getMyString();   
  16.         Toast.makeText(getApplicationContext(), "String at Second Activity  " + myGetString , Toast.LENGTH_LONG).show();  
  17.     }  
  18.       
  19.     @Override  
  20.     public void onBackPressed() {  
  21.         // do something on back.  
  22.   
  23.         finish();  
  24.           
  25.         return;  
  26.     }  
  27.       
  28.     protected void onDestroy() {  
  29.         super.onDestroy();       
  30.          
  31.     }  
  32.   
  33. }  
AndroidManifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.globalclassdemo"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="21" />  
  10.       
  11.     <application  
  12.           
  13.         android:name="com.example.globalclassdemo.MyGlobalClass"  
  14.           
  15.         android:allowBackup="true"  
  16.         android:icon="@drawable/ic_launcher"  
  17.         android:label="@string/app_name"  
  18.         android:theme="@style/AppTheme" >  
  19.         <activity  
  20.             android:name=".MainActivity"  
  21.             android:label="@string/app_name" >  
  22.             <intent-filter>  
  23.                 <action android:name="android.intent.action.MAIN" />  
  24.   
  25.                 <category android:name="android.intent.category.LAUNCHER" />  
  26.             </intent-filter>  
  27.         </activity>   
  28.       
  29.         <activity android:name="com.example.globalclassdemo.SecondActivity"></activity>  
  30.           
  31.           
  32.           
  33.     </application>  
  34. </manifest>  
Output

demo

string

 

Up Next
    Ebook Download
    View all
    Learn
    View all