0

I'm trying to save uuid in application memory. To do this I perform to use Shared Preferences and use this guide from developer.android.com From this place exactly PLACE

private fun setPreferences(response: JSONObject) {
        val sharedPref = this.getPreferences(Context.MODE_PRIVATE) ?: return
        with(sharedPref.edit()) {
            putString(
                getString(R.string.PREFERENCE_FILE_KEY),
                response.get("uuid").toString()
            )
            apply()
        }

This is my strings.xml file

<resources>
    <string name="app_name">Book of recipes</string>
    <string name="prompt_login">Login</string>
    <string name="prompt_password">Password</string>
    <string name="prompt_confirm_password">Confirm password</string>
    <string name="action_sign_in_short">Sign in</string>
    <string name="have_an_account">Already have an account?</string>
    <string name="dont_have_an_account">Don\'t have an account?</string>
    <string name="rabbits_with_carrots">Rabbits with carrots</string>
    <string name="login_error">Login must be 4–12 characters long</string>
    <string name="password_error">Password must be 4–16 characters long</string>
    <string name="password_error_reconciliation">Wrong password re-entered</string>
    <string name="characters_check_error">Field contains invalid characters</string>
    <string name="PREFERENCE_FILE_KEY"></string>
</resources>

After some tries to understand why this isn't working I write this lines of code in my setPreference fun

println("UUID : " + response.get("uuid").toString())

        println("Preference file key : " + getSharedPreferences(
            getString(R.string.PREFERENCE_FILE_KEY), Context.MODE_PRIVATE))

        println("R string : " + applicationContext.getString(R.string.PREFERENCE_FILE_KEY))

And take as response this :

I/System.out: UUID : 2c912ffb-01c0-430f-91ca-4ebe7d663225
I/System.out: Preference file key : android.app.SharedPreferencesImpl@638299c
    R string : 

So question , why I take this result when I trying to get shared preferences and how I can put this uuid in strings.xml file and take it back?

2 Answers2

1

The error is that you haven't set a key for you preference, change <string name="PREFERENCE_FILE_KEY"></string> to <string name="PREFERENCE_FILE_KEY">whatever</string>. whatever (or any other word) is the identifier of the variable you are saving, called a key.

I think you have misunderstood what you can achieve with SharedPreferences, you can't and there's no point in changing strings.xml at runtime as it is read only at compile time. With SharedPreferences you can save variables in internal storage and retrieve them later on, even if your app has stopped running in the meantime.

 

Save example:

fun Context.setSharedPreference(prefsName: String, key: String, value: String) {
    getSharedPreferences(prefsName, Context.MODE_PRIVATE)
        .edit().apply { putString(key, value); apply() }
}

 

Load example:

fun Context.getSharedPreference(prefsName: String, key: String): String {
    getSharedPreferences(prefsName, Context.MODE_PRIVATE)
        ?.getString(key, "Value is empty!")?.let { return it }
    return "Preference doesn't exist."
}
rtsketo
  • 1,168
  • 11
  • 18
  • I add value to the string , but my method still not working. But all is working fine with your methods. I think all problem in that my method don't have field that accept prefsName. Thanks you a lot for this answer – Belkov Dmytro Jul 05 '20 at 19:44
  • In what you wrote initially, you can't use `getPreferences` for storing and `getSharedPreferences` for retrieving. It has to be the same function in order to access the same preferences. `getPreferences` can be accessed by only one activity, while `getSharedPreferences` can be accessed by all the activities of your app. There's also the `getDefaultSharedPreferences` in which you don't have to specify a prefsName (For AndroidQ+ see: https://stackoverflow.com/questions/56833657/preferencemanager-getdefaultsharedpreferences-deprecated-in-android-q/56911496). – rtsketo Jul 05 '20 at 21:13
0

You missed <string name="PREFERENCE_FILE_KEY">UDID_KEY</string>. Update your string file from <string name="PREFERENCE_FILE_KEY"></string> to <string name="PREFERENCE_FILE_KEY">UDID_KEY</string> as shared prefs uses uniques key to save and retrive respective value.

'UDID_KEY' this string key could be anything you want. But for retriving the UDID back you need to use the same key which you used to save the data with for example UDID_KEY here.