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.
-
2did you try anything? any code? – Ankur Khandelwal Jan 31 '17 at 18:33
-
Is it the very next activity you are trying to pass the value ? – OBX Jan 31 '17 at 18:34
-
Yes I tried. I know how to store value to SharedPreferences but I don't know how to get stored value. – Ramon Darene Dela Cruz Jan 31 '17 at 18:35
-
create shared preference class, store data then use it every where – Iqbal Rizky Jan 31 '17 at 18:35
-
It is the very next activity but I'm using my background worker to open an intent – Ramon Darene Dela Cruz Jan 31 '17 at 18:36
-
Can you give me example on how to use it on another activity? – Ramon Darene Dela Cruz Jan 31 '17 at 18:36
-
2Possible duplicate of [How to use SharedPreferences in Android to store, fetch and edit values](http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – Al Lelopath Jan 31 '17 at 18:40
-
i wrote some answer for you try – Iqbal Rizky Jan 31 '17 at 18:41
4 Answers
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.

- 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
// 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", "");

- 247
- 1
- 5
-
this is the code to put string? loginPrefsEditor.putString("username", username); – Ramon Darene Dela Cruz Jan 31 '17 at 18:38
-
I would be able to get the stored value if I put, String name = sharedpreferences .getString("username", ""); – Ramon Darene Dela Cruz Jan 31 '17 at 18:39
-
yes right , you also need to use loginPrefsEditor.commit() statement to save the value in pref store – Anurag Aggarwal Jan 31 '17 at 18:40
-
Okay I will try that. If I use this, SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); Editor editor = sharedpreferences.edit(); editor.putString("key", "value"); editor.commit(); To get value of my username in edit text, I can use the stored value even if my intent is in the background worker? – Ramon Darene Dela Cruz Jan 31 '17 at 18:41
-
-
I'm using intent on onPostExecute() of my background worker to check the result of my php code and open another activity. – Ramon Darene Dela Cruz Jan 31 '17 at 18:46
-
onPostExecute() runs on UI thread so you can start a new activity from this method . If you want to get the saved value from shared preference , it will work on UI as well as worker thread – Anurag Aggarwal Jan 31 '17 at 18:48
-
I cant get stored value of SharedPreferences in background worker? – Ramon Darene Dela Cruz Jan 31 '17 at 18:51
-
paste your entire code here then we will be able to help you , though you can use shared preference in whatever thread you want – Anurag Aggarwal Jan 31 '17 at 18:58
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

- 352
- 3
- 16
-
If I store a username on SharedPreferences, I can use it in any activity? What are the limits of SharedPreferences? Will username be erased when I close application? – Ramon Darene Dela Cruz Jan 31 '17 at 18:48
-
-
-
@ntaloventi not true. It will only be deleted if the user clears ALL application data, not only cache. – gi097 Jan 31 '17 at 19:07
-
-
I used sample above cause my apps is trigger by web also, so if change on web apps, I should clear then store new data on android apps, that's why I'm using clear – Iqbal Rizky Jan 31 '17 at 19:12
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.

- 7,313
- 3
- 27
- 49
-
Do I need to put final SharedPreferences preferences = PreferenceManager .getDefaultSharedPreferences(ApplicationLoader.applicationContext); On every activity that I'm going to get value from? – Ramon Darene Dela Cruz Jan 31 '17 at 19:03
-
-
-
private SharedPreferences loginPreferences; loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE); String username = loginPreferences.getString("username", ""); I use this code. – Ramon Darene Dela Cruz Jan 31 '17 at 19:29
-
-