What's the point of having a default value in sharedPref.getString?

后端 未结 4 1986
一个人的身影
一个人的身影 2021-01-17 19:41

I\'m accessing my Android apps SharedPreferences via

private val sharedPref = PreferenceManager.getDefaultSharedPreferences(context)`

4条回答
  •  花落未央
    2021-01-17 20:15

    The fact is simple, just imagine you haven't saved any value regarding to that key(for your case 'MyKey') and tried to get the value for that key(for your case 'MyKey'). What will SharedPreference return ? It will simply return the default value.

    You will see that, you must assign null or any other string to default for String type, 0 or any other int value to default for integer type and true or false default value for bolean type. I hope you got the answer.

提交回复
热议问题