0

I have one activity that sets various variables to either true or false, to be used as settings for other activities. I need to be able to call the state of these variables in my other activities but I can't figure out how. I know for strings I can use

getApplicationContext().getResources().getString(R.string.stringName);

but the same thing won't work for Boolean. Someone suggested using

activityName.variableName

but that won't work either. Any suggestions?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
MendelumS
  • 91
  • 1
  • 8

2 Answers2

2

Instead of static variable or application variable use SharedPreference to achieve this which also persist on application close.

SettingsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    ....

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("YOUR_KEY1", true);
    editor.putBoolean("YOUR_KEY2", false);
    editor.putBoolean("YOUR_KEY3", true);
    editor.commit();

    ....
}

Then in other activity or fragment use getBoolean() to retrieve those data.

OthersActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    ....

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    //here false is the default value if key is missing 
    boolean value1 = sharedPreferences.getBoolean("YOUR_KEY1", false);
    boolean value2 = sharedPreferences.getBoolean("YOUR_KEY2", false);
    boolean value3 = sharedPreferences.getBoolean("YOUR_KEY3", false);

    ....
}
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
  • This looks perfect, how do I initialise the keys? At the moment I initialised my boolean values as ```private static boolean``` – MendelumS Jan 02 '20 at 20:30
  • Here keys should be any constant. Create separate class like `Utils` and create constant like `public static final String KEY1 = "key1" ;` Now use it like `Utils.KEY1`. Hope you understand. Please hit the upvote as well. Thanks – Md. Asaduzzaman Jan 02 '20 at 23:02
1

you can make global variables using many ways most common two

1- use of Application class

public class MyApplication extends Application {

    private String someVariable;

    public String getSomeVariable() {
        return someVariable;
    }

    public void setSomeVariable(String someVariable) {
        this.someVariable = someVariable;
    }
}

sure don't forgt to declare in manifest file

<application 
  android:name=".MyApplication" 
  android:icon="@drawable/icon" 
  android:label="@string/app_name">

How to use?

// set
((MyApplication) this.getApplication()).setSomeVariable("foo");

// get
String s = ((MyApplication) this.getApplication()).getSomeVariable();

2- by using Extra as a variable to be passed from activity to others with help of Intent

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);

to read it in second activity use

String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");

for setting screens suggest to use SharedPreference you can learn how to use from here

Moustafa EL-Saghier
  • 1,721
  • 1
  • 13
  • 43