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”
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)); } }); } }
Next SecondClass.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)); } }); } }
Now the GlobalClass.java which holds the global variable.
You can have any datatype as global variable….
package pack.coderzheaven.test; import android.app.Application; class GlobalClass extends Application{ String global_var1 = "Value1"; }
Please check your logcat to see the global value changes…..
Get more ANDROID posts from here
Happy coding………
Please leave your valuable comments………
nice post bro. waiting for the next part to show up
nice concept..:)
very nice tutorials….
Pingback: display firstname and lastname after login success - Android Forums