4

I'm wondering how I would go about accessing the same int variable between all my Activity classes in my Android app. My situation is that I have a variable that represents a number of points and I've placed it in its own class and I want the value to be the same between every Activity that uses it.

When the user gets a point, it increases by 1 so let's say the user gets 12 points, I want it to be the same throughout all the Activitys.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
Kayden Rice
  • 53
  • 1
  • 6
  • Check this [answer](http://stackoverflow.com/a/4878259/5891893) for best common ways to share data between activities – Sally Feb 15 '16 at 13:47

5 Answers5

8

STEP 1:

Extend all your Activitys from a common BaseActivity class.

STEP 2:

Put your int variable in BaseActivity and add the protected and static qualifiers to the int variable:

public class BaseActivity extends Activity{

    ....
    ....
    protected static int points;
    ....
    ....

}

Now you can access the points variable in every Activity and it will have the same value.

There is no need to use a singleton here as the other answers are suggesting. Its much simpler to have a common static variable. Its programming 101.

Try this. This will work.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • I specified that use singleton is one of the best mode to share vars. Your solution works too, it's only an other mode to use static vars. Your example is good to share only between Activity, singleton is a tipical design pattern, all developer should know it. – ZanoOnStack Oct 22 '15 at 13:27
  • 1
    I like Occam's Razor. It applies here, look it up ... :) – Yash Sampat Oct 22 '15 at 13:31
7

You can use a singleton object class. But it is a java elementary pattern.

public class MySingletonClass {

    private static MySingletonClass instance;

    public static MySingletonClass getInstance() {
        if (instance == null)
            instance = new MySingletonClass();
        return instance; 
    }

    private MySingletonClass() {
    }

    private int intValue;

    public int getIntValue() {
        return intValue;
    }

    public void setIntValue(int intValue) {
        this.intValue = intValue;
    }
}

And you can use every where

MySingletonClass.getInstance().getInt;

and

MySingletonClass.getInstance().setInt(value);

It's not the fastest mode, but it's one of the best. In fact you can add how many var you want, and you can access from everywhere, also in not-Activity class

ZanoOnStack
  • 389
  • 2
  • 11
0

You can do it by defining variable in application context and use that variable as a Global variable. You just set value to that variable and get value on any activity / broadcast recieiver / service in application context(environment).

Every Application has a context, and Android guarantees that Application context will exist as a instance across your application.

The way to do this is to create a class and extends with android.app.Application, and specify your class in the application tag in your AndroidManifest.xml file. Android will create an instance of that class and make it available for your entire application context. You can get object of your class on any activity / broadcast reciever / service in application context(environment) by Context.getApplicationContext() method.

N:B: You can achieve this by creating static variable but it is not the good way it influencing towards memory leaks

You can see code example here

Zahan Safallwa
  • 3,880
  • 2
  • 25
  • 32
0

Create a Java class that extends the Android Application Class. Put your int variable there as a static. Then create your getter and setter to access the variable anywhere from your app.

public class MyApplication extends Application{
    private static int someInteger;
    //setter
    //getter
}

From any android class call your variable like this.

MyApplication.getSomeInteger()
MyApplication.setSomeInteger(Int someInteger)
TerNovi
  • 390
  • 5
  • 16
0

For this, use SharedPreference, more safe

Save in SharedPreference your points, and rescue them when you need.

MZapatae
  • 29
  • 2