Preferência comum compartilhada (CURD) SharedPreference: para armazenar dados na forma de pares de valor-chave com uma classe Kotlin simples.
var sp = SharedPreference(this);
Armazenamento de dados:
Para armazenar dados String, Int e Boolean, temos três métodos com o mesmo nome e parâmetros diferentes (Sobrecarga de método).
save("key-name1","string value")
save("key-name2",int value)
save("key-name3",boolean)
Recuperar dados: Para recuperar os dados armazenados em SharedPreferences, use os seguintes métodos.
sp.getValueString("user_name")
sp.getValueInt("user_id")
sp.getValueBoolean("user_session",true)
Limpar todos os dados: para limpar todas as SharedPreferences, use o código a seguir.
sp.clearSharedPreference()
Remova dados específicos:
sp.removeValue("user_name")
Classe de preferência compartilhada comum
import android.content.Context
import android.content.SharedPreferences
class SharedPreference(private val context: Context) {
private val PREFS_NAME = "coredata"
private val sharedPref: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
//********************************************************************************************** save all
//To Store String data
fun save(KEY_NAME: String, text: String) {
val editor: SharedPreferences.Editor = sharedPref.edit()
editor.putString(KEY_NAME, text)
editor.apply()
}
//..............................................................................................
//To Store Int data
fun save(KEY_NAME: String, value: Int) {
val editor: SharedPreferences.Editor = sharedPref.edit()
editor.putInt(KEY_NAME, value)
editor.apply()
}
//..............................................................................................
//To Store Boolean data
fun save(KEY_NAME: String, status: Boolean) {
val editor: SharedPreferences.Editor = sharedPref.edit()
editor.putBoolean(KEY_NAME, status)
editor.apply()
}
//********************************************************************************************** retrieve selected
//To Retrieve String
fun getValueString(KEY_NAME: String): String? {
return sharedPref.getString(KEY_NAME, "")
}
//..............................................................................................
//To Retrieve Int
fun getValueInt(KEY_NAME: String): Int {
return sharedPref.getInt(KEY_NAME, 0)
}
//..............................................................................................
// To Retrieve Boolean
fun getValueBoolean(KEY_NAME: String, defaultValue: Boolean): Boolean {
return sharedPref.getBoolean(KEY_NAME, defaultValue)
}
//********************************************************************************************** delete all
// To clear all data
fun clearSharedPreference() {
val editor: SharedPreferences.Editor = sharedPref.edit()
editor.clear()
editor.apply()
}
//********************************************************************************************** delete selected
// To remove a specific data
fun removeValue(KEY_NAME: String) {
val editor: SharedPreferences.Editor = sharedPref.edit()
editor.remove(KEY_NAME)
editor.apply()
}
}
Blog:
https://androidkeynotes.blogspot.com/2020/02/shared-preference.html