How to use Global variables in ANDROID?

By | May 17, 2011

Hi all…….
In today’s exampl e I will show you how to deal with global variables in ANDROID.
You know global variables are those variables that you can access across any variables.
It’s value becomes changed when you change it in any of the classes in your project. These types of variables act as like cookies in a browser to store your login information across different pages. Now Let’s see how to implement this………
For this demo I am using three classes, one is the global class which has the global variable and other two normal classes.
In the global class I have a variable named “global_var” which I am accessing across the other two classes and I am changing the value across the classes. Please check the Logcat for seeing the output.

First class “Global_var_test.java
[java]
package pack.coderzheaven.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Global_var_test extends Activity {

Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);

b = (Button)findViewById(R.id.Button01);
GlobalClass global = new GlobalClass();
System.out.println("var1 value in first class before changing = " + global.global_var1);
global.global_var1 = "value2";
System.out.println("var1 value in first class after changing = "; + global.global_var1);

b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Global_var_test.this, SecondClass.class));
}
});
}
}
[/java]

Next SecondClass.java
[java]
package pack.coderzheaven.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SecondClass extends Activity{

Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);

b = (Button)findViewById(R.id.Button01);

GlobalClass global = new GlobalClass();
System.out.println("var1 value in second class before changing = " + global.global_var1);
global.global_var1 = "value3";
System.out.println("var1 value in second class after changing = " + global.global_var1);

b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(SecondClass.this, SecondClass.class));
}
});
}

}
[/java]

Now the GlobalClass.java which holds the global variable.
You can have any datatype as global variable….

[java]
package pack.coderzheaven.test;

import android.app.Application;

class GlobalClass extends Application{

String global_var1 = "Value1"

}
[/java]

Please leave your valuable comments………

5 thoughts on “How to use Global variables in ANDROID?

  1. WiG

    Hi, this example is very interesting, but IMHO in the both activities you create two separated instances of the GlobalClass type and the value of global.global_var1 from Global_var_test isn’t visible out of this class…

    To test this, I have created very simple class:

    package com.example.android.skeletonapp;
    import android.app.Application;
    class Globals extends Application
    {
    int intTestVar; // the value is by default set to 0
    }

    and in the activity #1 I wrote:

    Globals classGlobals = new Globals();
    classGlobals.intTestVar = 3;

    Then I have started activity #2 and tried to get global value:

    Globals classGlobals = new Globals();
    temp = classGlobals.intTestVar;

    and temp still equals 0…

    What I’ve done wrong?

    Regards, WiG

    Reply
    1. James Post author

      Because I think each time when you create a new object the TestVar will be set to zero.

      Reply
  2. Rossa

    Because its wrong, you need to use it like this:
    instead of the line:

    GlobalClass global = new GlobalClass();

    use this:

    GlobalClass global = (GlobalClass)getApplicationContext();

    This is essentially a singlton so do not use the new Class sintax or else it wont work…

    Reply
  3. Pingback: How to use global variables in android? -Part 2 | Coderz Heaven

Leave a Reply

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