2

Basically my question is I want to set lets say a string to A or B that is chosen by the user on the first screen. And then have this string variable be saved to be used on other actives else where in the app. I have see the posts and many like in Android global variable.

But the variable needs to be set and the gotten on each activity the variable isn't saved, once and then can be used everywhere?

How could this be done?

I hope i have explained this well enough, as my question differs from the one above.

The variable is not final it can be changed on the first activity but then I want to use it on the following activities, with out having to pass it with intent to each one.

Thanks for the help in advance.

Community
  • 1
  • 1
iqueqiorio
  • 1,149
  • 2
  • 35
  • 78

8 Answers8

4

You can use a public class with static String variable or passing the variable to another Activity with putExtra method.

Example :

public class Global {
    public static final String DEVELOPER_KEY;
    public static final String PLAYLIST_ID;
}
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
2

I have made one class for same purpose :

public class Constant {

    public static int NUMBER_OF_TILES;

    public static String setTilesId[] = {};

    public static String TilesDesc[] = {};

    public static String Image[] = {};

    public static String TilesName;

    public static int numberofbox = 0;

    public static boolean isLogedIn = false;

    public static String TilesSize = null;

    public static boolean from_activity = true;

    public static String URL_FOR_PRODUCT_ACTIVITY;

}

Now i can use this variables from anywhere in my APP.

Shvet
  • 1,159
  • 1
  • 18
  • 30
  • I made a java class called constants and then in my main activity tried to set one, and got `cannot find symbol` – iqueqiorio Dec 05 '14 at 05:40
  • There are 2 ways to call variable from another class. Either import that class in mainactivity or use `Constant.TilesName`. – Shvet Dec 05 '14 at 05:47
  • By using `Constant.TilesName` it will automatically import Constant class in your mainactivity. – Shvet Dec 05 '14 at 05:48
  • how could I import the class? So I don't need `Constant.name` and I can just use `name` – iqueqiorio Dec 05 '14 at 05:56
  • I tried something like `import android.activyt.calssname` – iqueqiorio Dec 05 '14 at 05:57
  • yes. To import class manually you have to write it like that `import fullname of class`. my `constant` class in my package `com.scs.riga.library` so i have imported it as a `import com.scs.riga.library.Constant` and also i use eclipse. – Shvet Dec 05 '14 at 06:02
1

A.java code:

  package p2;
  public class A{

         public static String abc;

  }

After declaring the static string in class A use it anywhere in the package using the class name as it is static so value will remain whatever you update it to.

B.java code :

 package p1;
 public class B{

       public void methodTest(){

                String s = A.abc;    

      }

 }
varun
  • 1,473
  • 1
  • 9
  • 15
1

You can put your variable in SharedPreferences. As it exactly matches you requirement. You can put your variable in first screen and use it anywhere in your application. Also it is not final, you can change whenever you want it will just overwrite existing value of variable.

And MOST important thing is it will be preserved even after application is closed.

So whenever user starts your application next time you can use that variable.

Android
  • 179
  • 1
  • 7
0

Make your string public static

Ex:public static String myvar="hey"//This will be available across all classes

archon92
  • 447
  • 3
  • 13
0
Instead of using static variable You can simply use Shared Preferences because after some time when your application is in background your static value become free automatically by garbage collector and become null


    public void setString(Context context, String value) {
            SharedPreferences prefs = context.getSharedPreferences("setString",
                    0);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("getString", value);
            editor.commit();

        }

        public String getString(Context context) {
            SharedPreferences prefs = context.getSharedPreferences("setString",
                    0);
            String value = prefs.getString("getString", null);

            return value;

        }
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31
0

You can also usw SharedPreferences:

SharedPreferences sets =getSharedPreferences(Choose_a_name, 0);
SharedPreferences.Editor editor=sets.edit();
editor.put string("from user", your string);
editor.commit();

And in your other activities you can retrieve, change and save them again. In that way, you can provide the customized data from the user over the activity lifecycle. Even If your task is completely killed, your data is available on the next launch.

To retrieve the data from another activity:

 SharedPreferences sets = getSharedPreferences(yourPrefName,0);
String your string = sets.get string("from user", default value);

I want to give you alternative ideas comparing to intents or static classes. Hope it helps :)

Mike
  • 857
  • 1
  • 7
  • 11
0
class SharedPref
{

public void setString(Context context, String value) {
            SharedPreferences prefs = context.getSharedPreferences("setString",
                    0);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("getString", value);
            editor.commit();

        }

        public String getString(Context context) {
            SharedPreferences prefs = context.getSharedPreferences("setString",
                    0);
            String value = prefs.getString("getString", null);

            return value;

        }
}


and use this class like

SharePref pref=new SharedPref();

pref.setString("Hellooo");


String value=pref.getString();
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31