1

What is the best way to save null value of data type "Integer" in Shared Preferences. I can find putInt(which does not allow null values); but no putInteger.

Kindly suggest the proper way to achieve this.

Saqib
  • 377
  • 4
  • 7

1 Answers1

3

https://developer.android.com/reference/android/content/SharedPreferences.Editor#putInt(java.lang.String,%20int)

As long as the putInt function only accept the int primitive type as its argument. So I think you can't put null value in there.

My suggestion is you should try putString instead of it. And when you get the value from SharePreference, just parse it into int value.

My solution:

String value = mSp.getString("YOUR_KEY", "");
if (value.equals("") {
     // Which means it is 'null' number
} else {
     int number = Integer.parseInt(value);
}
Brian H.
  • 1,603
  • 11
  • 16