iqueqiorio
iqueqiorio

Reputation: 1187

Android - Set variable once and use everywhere in app

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.

Upvotes: 2

Views: 1957

Answers (8)

varun
varun

Reputation: 1523

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;    

      }

 }

Upvotes: 1

Android
Android

Reputation: 179

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.

Upvotes: 1

Rohit Heera
Rohit Heera

Reputation: 2737

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();

Upvotes: 0

Mike
Mike

Reputation: 857

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 :)

Upvotes: 0

Rohit Heera
Rohit Heera

Reputation: 2737

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;

        }

Upvotes: 0

Shvet
Shvet

Reputation: 1201

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.

Upvotes: 2

archon92
archon92

Reputation: 447

Make your string public static

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

Upvotes: 0

Blaze Tama
Blaze Tama

Reputation: 10948

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;
}

Upvotes: 4

Related Questions