Introduction

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