0

Following is the code I used to save SharedPreference:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();

editor.putString("deliveryId", obj.get("deliveryId").toString());
editor.commit();

This is not working in some devices, but working in some devices. It is working on KitKat but doesn't work on JellyBean and Lollipop.

Following is the code I used to get the data:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String deliveryId = preferences.getString("deliveryId", "0");

Always gives the default value, i.e. 0, on some devices.

Sufian
  • 6,405
  • 16
  • 66
  • 120
irfan
  • 869
  • 3
  • 12
  • 25

1 Answers1

1

I think default preference creating problem for you try like below for saving SharedPreferences

SharedPreferences preferences = getSharedPreferences("<Pref Name>", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();

editor.putString("deliveryId", obj.get("deliveryId").toString());
editor.commit();

to get the data:

SharedPreferences preferences = getSharedPreferences("<Pref Name>", MODE_PRIVATE);
String deliveryId = preferences.getString("deliveryId", "0");

Happy Coding!

Shrenik Shah
  • 1,900
  • 1
  • 11
  • 19
  • Why is this better than **getDefaultSharedPreferences**? – IgorGanapolsky Dec 07 '16 at 14:32
  • 1
    @IgorGanapolsky frankly speaking, i do not have exact idea why not to use getDefaultSharedPreferences but you can go through below link for more details https://code.google.com/p/android/issues/detail?id=14359 – Shrenik Shah Feb 02 '17 at 05:04