0

I know this question already have tons of answer in SO but I haven't found the answer to my problem.

Here is my code:

SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);

//Creating editor to store values to shared preferences
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();

//Adding values to editor
editor.putBoolean(Config.LOGGEDIN_SHARED_PREF, true);
editor.putString(Config.PHONE_SHARED_PREF, phoneNo);
Log.d("debug", "config "+Config.PHONE_SHARED_PREF);
Log.d("debug", "config "+phoneNo);

//Saving values to editor
editor.apply();
editor.commit();

What I understand is, editor.putString(Config.PHONE_SHARED_PREF, phoneNo) means save the value of phoneNo into PHONE_SHARED_PREF. Please correct me if I'm wrong.

But when in Log, Config.PHONE_SHARED_PREF printed the default value, instead of the new value assigned in phoneNo. That's mean the value of phoneNo not correctly saved, no?

Can someone explain to me what's wrong with my code? :/

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
August
  • 309
  • 1
  • 5
  • 15
  • `commit()` and `apply()` are the same, with the latter not returning a boolean value. Remove either and try again. You can get the value using `editor.getString(Config.PHONE_SHARED_PREF, "")`. The 2nd param is returned if the key does not exists. – Zhi Kai Oct 08 '16 at 10:02
  • 1
    Sorry, try `sharedPreferences.getString(...)`. – Zhi Kai Oct 08 '16 at 10:09
  • it returns nothing (2nd param) – August Oct 08 '16 at 10:14
  • If that's the case, it means the preference doesn't exists. Use the `getAll()` method to check if your preference contains the keys and values you wanted. – Zhi Kai Oct 08 '16 at 10:19

2 Answers2

2
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);

//Creating editor to store values to shared preferences
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();

//Adding values to editor
editor.putBoolean(Config.LOGGEDIN_SHARED_PREF, true)
      .putString(Config.PHONE_SHARED_PREF, phoneNo)
      .commit(); // returns true if successfully saved. 

Log.d("debug", "config " + sharedPreferences.getString(Config.PHONE_SHARED_PREF, "");

Try the above.

Zhi Kai
  • 1,549
  • 1
  • 13
  • 31
  • this one works! thanks! mind to explain a little bit? still new in shared preference :) – August Oct 08 '16 at 10:26
  • 1
    Everything you've done in your code seems correct, except the part where you include `apply()` and `commit()`. Essentially, you'll only need to use either one, not both. You can refer to http://stackoverflow.com/questions/5960678/whats-the-difference-between-commit-and-apply-in-shared-preference to understand the difference between the two. – Zhi Kai Oct 08 '16 at 10:29
  • Please accept the answer if possible, it would be of help to others in the future if they're looking for similar help. :] – Zhi Kai Oct 08 '16 at 10:38
1

Try this.

SharedPreferences sharedPreferences = getActivity().getSharedPreferences(Config.SHARED_PREF_NAME, Activity.MODE_PRIVATE);

sharedPreferences.edit().putBoolean(Config.LOGGEDIN_SHARED_PREF, true).commit();
sharedPreferences.edit().putString(Config.PHONE_SHARED_PREF, phoneNo).commit();
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98