Jump to content

Android Studio, accessing variables from different activities

Lucaz97

I am working on a simple app to exercise after a few moths in which i havent coded at all ... 

I made a Tic Tac Toe (crosses and circles) app, there is a main activity wih a menu, the user can choose between playing with an AI (can t win ) or with a friend. I also made a setting voice in the menu because i wanted to try some things... 

The development of the game part is all done and works pretty well. I am now to the part of experiments with the settings. 

I thought i would have liked the user to be able to change the color of the background and to disable the music ( that i managed to add with a MediaPlayer object). 

The problem is that I am struggling finding a way to do that. 

I would need to access from the settings activity variables of the other activities and I can't see how i could do that... I also considered to make only one big activity for all the app, it is going to be a bit messy but it would solve this problem... 

Anyone with any idea, suggestion, or solution ?

Thanks, Luca 

Intel i7 6700k @4.6 1.330v  cooled with H110i GT // 16Gb Kingston Fury DDR4 ram @2133MHz // Rx 480 Nitro+ // 2TB + 1 TB Hdd  // 250 GbSSD // on an Asus Z170-A // powered with Corsair RM750i // all  inside a Corsair 600C

Link to comment
Share on other sites

Link to post
Share on other sites

To access settings from other classes in Java, you either need to made them public and static (NOT RECOMMENDED), or private and static with public accessors. So for example:

 

public class MyClassWithSettings {

    private static int _myVariable = 5;

    //Access the variable
    public static int myVariable() { return MyClassWithSettings._myVariable; }

    //Set the variable
    public static void setMyVariable(int myVariable) {
        //Any error checking code for invalid values...
        MyClassWithSettings._myVariable = myVariable;
    }
}

 

Then when you want to access it outside of the class, you can use

 

MyClassWithSettings.setMyVariable(100);
//later...

if (MyClassWithSettings.myVariable() == 5) {
    //Code
} else if (MyClassWithSettings.myVariable() == 100) {
    //Other code
}

etc.

 

 

EDIT: This is assuming you only have 1 of these classes, because the static qualifier means there only exists 1 of the variable (so if you make a change to the variable, it changes MyClassWithSettings.myVariable() in every instance. You can of course drop all of the static identifiers above, but then you will only be able to access from an instance of that class. So if you didn't include any of the static identifiers, MyClassWithSettings.myVariable() would not compile. You'd instead need to initialize the variable in the class constructor:

public class MyClassWithSettings {

    private int _myVariable;
    
    public MyClassWithSettings(int myVariable) {
        //Constructor
        _myVariable = myVariable;
    }
    
    //Access the variable
    public int myVariable() { return _myVariable; }

    //Set the variable
    public void setMyVariable(int myVariable) {
        //Any error checking code for invalid values...
        _myVariable = myVariable;
    }
} 

 

Then later, you can access the object after creating an instance of it...

MyClassWithSettings mcws = new MyClassWithSettings(5);
if (mcws.myVariable() == 5) {
    //As before
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Gotcha... I was just missing the static thing... Thank you very much!

Intel i7 6700k @4.6 1.330v  cooled with H110i GT // 16Gb Kingston Fury DDR4 ram @2133MHz // Rx 480 Nitro+ // 2TB + 1 TB Hdd  // 250 GbSSD // on an Asus Z170-A // powered with Corsair RM750i // all  inside a Corsair 600C

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×