Preferenza condivisa comune (CURD) SharedPreference: per memorizzare i dati sotto forma di coppie valore-chiave con una semplice classe Kotlin.
var sp = SharedPreference(this);
Memorizzazione dei dati:
Per memorizzare i dati String, Int e Boolean abbiamo tre metodi con lo stesso nome e parametri differenti (Method overloading).
save("key-name1","string value")
save("key-name2",int value)
save("key-name3",boolean)
Recupera dati: per recuperare i dati archiviati in SharedPreferences utilizzare i seguenti metodi.
sp.getValueString("user_name")
sp.getValueInt("user_id")
sp.getValueBoolean("user_session",true)
Cancella tutti i dati: per cancellare l'intero SharedPreferences utilizzare il codice seguente.
sp.clearSharedPreference()
Rimuovi dati specifici:
sp.removeValue("user_name")
Classe di preferenza condivisa comune
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