-1

I am making a boolean variable value in one class and accessing the status of that variable in another class , on the basis of the boolean variable status my list view shows items,

So my question is

  1. how to create a global boolean varible
  2. how to pass it to another class
  3. how the second class check it
Ashish Jambhulkar
  • 1,374
  • 2
  • 14
  • 26
  • you can create a static field, for example `public static boolean variable` – Desert Jul 14 '13 at 12:43
  • why do you need it to be a true global? Assuming you are talking about passing it between Activities, you can easily pass the variable value in the intents you will be using to create the second activity. – Rajeev Jul 14 '13 at 12:43
  • possible duplicate question check this [Android Global](http://stackoverflow.com/questions/1944656/android-global-variable) – Srikanth Roopa Jul 14 '13 at 12:48

3 Answers3

4

1. Variable in class A

public class A{
    public String aClassVar="hello";
    }

Using it in Class B

A obj=new A();
String inBClass=obj.aClassVar;

2. To pass data from one Activity to Another Activity you can use Intent remember your Class should extends Activity only than you will be able to pass data by using Intent

Example

Send Data From First Activity using :

Intent i = new Intent(this, SecondClassName.class);
i.putExtra("key", "Value");// key is used to get value in Second Activiyt
startActivity(i); 

Receive Data on Second Activity using:

Intent intent = getIntent();
String temp = intent.getStringExtra("key");// usr getStringExtra() If your extra data is represented as strings:

And you Must set Activity Name inside AndroidManifest.xml

like:

<activity android:name="yourPackageName.SecondClassName" />
ridoy
  • 6,274
  • 2
  • 29
  • 60
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
1

Let me suggest 3 options.

Do you want to pass a boolean variable between Android Activities? If so, You may want to use a Bundle. Yes, those little things given to Activities on onCreate(). You can pass variables of your own into these, in your case a boolean, with putBoolean() and getBoolean()

Would you prefer using Android's SharedPref? It's an interface for sharing small preferences, like boolean flags, between parts of your app and storing it for later.

Or, you could just implement a singleton class that has the boolean variable and other variables you need to store and check by different classes within your app.

user1555863
  • 2,567
  • 6
  • 35
  • 50
0

If you just want to access the value of an object or variable in another class, make it Static and then when you need its value, do like

public class tempClass {

   public void tempMethod() {

     boolean status = myClass.myVariable ; // where myVariable is static boolean varaible of myClass
  }
}

But make sure to access the variable after some value is stored in it.

If you want to send the value to another activity then send the value by using intent.

Eg.

Bundle myBund = new Bundle();

myBund.putBoolean(myBool);

Intent intent = new Intent(myClass.this, tempClass.class);

intent.putExtras("myKey",myBund);

startActivity(myBund);
Umer Farooq
  • 7,356
  • 7
  • 42
  • 67