1

I'am trying to store a value of edit text using shared preferences but I don't know how to get stored value of SharedPreferences() in another activity.

4 Answers4

1

If you are working in an App that may get complex further down the way, it is better to centralize your shared preferences. So I suggest to make a class for shared preferences like this:

Java Class

//SharedPreferences manager class
public class SharedPrefs {

//SharedPreferences file name
private static String SHARED_PREFS_FILE_NAME = "my_app_shared_prefs";

//here you can centralize all your shared prefs keys
public static String KEY_MY_SHARED_BOOLEAN = "my_shared_boolean";
public static String KEY_MY_SHARED_FOO = "my_shared_foo";

//get the SharedPreferences object instance
//create SharedPreferences file if not present


private static SharedPreferences getPrefs(Context context) {
    return context.getSharedPreferences(SHARED_PREFS_FILE_NAME, Context.MODE_PRIVATE);
}

//Save Booleans
public static void putBoolean(Context context, String key, boolean value) {
    getPrefs(context).edit().putBoolean(key, value).commit();       
}

//Get Booleans
public static boolean getBoolean(Context context, String key) {
    return getPrefs(context).getBoolean(key, false);
}

//Get Booleans if not found return a predefined default value
public static boolean getBoolean(Context context, String key, boolean defaultValue) {
    return getPrefs(context).getBoolean(key, defaultValue);
}

//Strings
public static void putString(Context context, String key, String value) {
    getPrefs(context).edit().putString(key, value).commit();
}

public static String getString(Context context, String key) {
    return getPrefs(context).getString(key, "");
}

public static String getString(Context context, String key, String defaultValue) {
    return getPrefs(context).getString(key, defaultValue);
}

//Integers
public static void putString(Context context, String key, int value) {
    getPrefs(context).edit().putInt(key, value).commit();
}

public static int getInt(Context context, String key) {
    return getPrefs(context).getInt(key, 0);
}

public static int getInt(Context context, String key, int defaultValue) {
    return getPrefs(context).getInt(key, defaultValue);
}

//Floats
public static void putFloat(Context context, String key, float value) {
    getPrefs(context).edit().putFloat(key, value).commit();
}

public static float getFloat(Context context, String key) {
    return getPrefs(context).getFloat(key, 0);

In your activity you may save SharedPreferences this way

Save a variable

//saving a boolean into prefs
SharedPrefs.putBoolean(this, SharedPrefs.KEY_MY_SHARED_BOOLEAN, booleanVar);

and you may retrieve your SharedPreferences this way

Retrieve a saved value

//getting a boolean from prefs
booleanVar = SharedPrefs.getBoolean(this,SharedPrefs.KEY_MY_SHARED_BOOLEAN);

This way you won't have scattered shared preference code, sometimes with different structure all over the App.

alexm
  • 1,285
  • 18
  • 36
  • Okay, I'm gonna try it. I'm still learning about android studio. I'am working on online ordering application with sms as my thesis right now. – Ramon Darene Dela Cruz Jan 31 '17 at 18:55
  • Do I need to declare SharedPreferences in the activity I'm going to get the stored value from? private SharedPreferences loginPreferences; private SharedPreferences.Editor loginPrefsEditor; Like this? – Ramon Darene Dela Cruz Jan 31 '17 at 19:02
  • Not exactly, you should create a new java class (right click on com.yourdomain.yourapp ->New->Java Class) with the first code section called SharedPrefs, in your activities you should just import SharedPrefs (import com.yourdomain.yourapp.SharedPrefs) and just save the value you want to keep like in the 2nd section and recover it like in the 3rd section above – alexm Jan 31 '17 at 19:07
  • Oh, okay I understand now. I don't need to put it in every activity because it is saved in a class. Thank you for this, this will make my codes organized. – Ramon Darene Dela Cruz Jan 31 '17 at 19:13
0
// store the value in preference store

    SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    Editor editor = sharedpreferences.edit();
    editor.putString("key", "value");
    editor.commit();

// get the value from preference store

    String name = sharedpreferences .getString("key", "");
Anurag Aggarwal
  • 247
  • 1
  • 5
0

here is my try, create class of SF

public class SFData {

public static SharedPreferences sf_login;
static SharedPreferences.Editor sf_login_edit;

public static void loginSF(Context ctx){
    sf_login = ctx.getSharedPreferences("login", 0);
    sf_login_edit = sf_login.edit();
}

public static void loginSFStore(Map<String, Object> mapData){
    loginSFClear();
    for (Map.Entry<String, Object> entry : mapData.entrySet()) {
        String keyName = entry.getKey();
        Object value = entry.getValue();
        if (value instanceof String) {
            sf_login_edit.putString(keyName, (String) value);
        }
        else if (value instanceof Boolean) {
            sf_login_edit.putBoolean(keyName, (boolean) value);
        }
        else if (value instanceof Integer) {
            sf_login_edit.putInt(keyName, (int) value);
        }
    }
    sf_login_edit.putBoolean("isLogin", true);
    sf_login_edit.commit();
}

public static void loginSFClear(){
    sf_login_edit.clear();
    sf_login_edit.commit();
}

}

sample of use Boolean login = SFData.sf_login.getBoolean("isLogin", false); please adapt to your code

Iqbal Rizky
  • 352
  • 3
  • 16
-1

First intialize SharedPreferences with a Context:

final SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(ApplicationLoader.applicationContext);

To save you could use this:

SharedPreferences.Editor editor = preferences.edit();
editor.putString("editText", yourEditText.getText().toString());
editor.commit();

Then to get the value inside another class, you could use:

String editText = preferences.getString("editText", "");

The second value "" represents a default value, if editText is not saved earlier, an empty String will be returned.

gi097
  • 7,313
  • 3
  • 27
  • 49