Home > Software design >  How do I use SharedPreferences to getString in Kotlin (android)?
How do I use SharedPreferences to getString in Kotlin (android)?

Time:01-10

I just can't figure out how to use this a fragment. The official documentation shows this example:

val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
val defaultValue = resources.getInteger(R.integer.saved_high_score_default_key)
val highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), defaultValue)

Replacing "getInt" with "getString" always returns ""

    val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE)
    val password = sharedPref?.getString("key", "")
    if (key == "") {
      // this always calls even when I change the value from the settings menu!
    } else {

    }

CodePudding user response:

This is the simple class use for store any data type(string,boolean,Int) in sharedpreference

class PrefUtil(context: Context) {
private val context: Context
val PREFS_NAME = "my_prefs"
fun setInt(key: String?, value: Int) {
    val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, 0)
    val editor: SharedPreferences.Editor = prefs.edit()
    editor.putInt(key, value)
    editor.apply()
}

fun getInt(key: String?, defValue: Int): Int {
    val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, 0)
    return prefs.getInt(key, defValue)
}

fun setString(key: String?, value: String?) {
    val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, 0)
    val editor: SharedPreferences.Editor = prefs.edit()
    editor.putString(key, value)
    editor.apply()
}

fun getString(key: String?): String? {
    val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, 0)
    return prefs.getString(key, "null")
}

fun setBool(key: String?, value: Boolean) {
    val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, 0)
    val editor: SharedPreferences.Editor = prefs.edit()
    editor.putBoolean(key, value)
    editor.apply()
}

fun getBool(key: String?): Boolean {
    val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, 0)
    return prefs.getBoolean(key, false)
}

fun getBool(key: String?, defaultValue: Boolean): Boolean {
    val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, 0)
    return prefs.getBoolean(key, defaultValue)
} 

init {
    this.context = context
}

}

and for store String use

PrefUtil(this).setString("Key","value")

for getting string from sharedPreference use

PrefUtil(this).getString("key")

CodePudding user response:

You need to insert a piece of data for this key first, and then call get method:

    val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE)
    with (sharedPref.edit()) {
      putString("key", "abc123456")
      apply()
   } 
    val password = sharedPref?.getString("key", "")
    if (password == "") {
      // ....
      Log.d(TAG,"password is empty.")
    } else {
      Log.d(TAG,">>>:$password")
    }

CodePudding user response:

You can try like this

//For Add String Value in sharedPreferences
val sharedPreferences = getSharedPreferences("key", MODE_PRIVATE) ?: return
with(sharedPreferences.edit()) {
    putString("yourStringKey", "Hello World")
    apply()
}

 //Here get enter string value from sharedPreferences other activity 
  val sharedPreferences1 = getSharedPreferences("key", MODE_PRIVATE) ?: return
  val string = sharedPreferences1.getString("yourStringKey", "hi") //"hi" is default value
Log.e("sharedPreferences1", "sharedPreferences1 Val is -->> $string")

CodePudding user response:

Turns out I actually needed to use SharedPreferences as I'm using a Settings Activity. Thanks for the answers though as they kind of helped to lead in the direction I needed.

  • Related