asdasd asd
asdasd asd

Reputation: 51

How to make Android java global string variable?

How do I make global string variable on android-java, and how to access it on diffrent activity?

Upvotes: 0

Views: 1349

Answers (3)

CHAN HAU YEEN
CHAN HAU YEEN

Reputation: 353

In res>values>strings.xml, add this global variable:

<string name="path">http://192.168.3.18</string>

Then in your activity, you can use

String path = getString(R.string.path);

to retrieve the value.

Upvotes: 1

CaptainTeemo
CaptainTeemo

Reputation: 116

Probably what you want is to store the string in somewhere in the app and wherever you need it recover it.

You can use SharedPreferences.

Store it:

SharedPreferences settings = getSharedPreferences("CoolPreferences", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("StringName", "StringValue");
// Commit the edits!
editor.commit();

Recover it:

SharedPreferences settings = getSharedPreferences("CoolPreferences", 0);
String silent = settings.getString("StringName", "DefaultValueIfNotExists");

You need a Context like and Activity to call getSharedPreferences()

Upvotes: 0

SimonSays
SimonSays

Reputation: 10977

You usually don't make global string variables in Android. What you should do is to use a resource file. Check out the documentation.

Upvotes: 0

Related Questions