Ortak paylaşılan tercih (CURD) SharedPreference: Verileri, basit bir Kotlin sınıfıyla değer-anahtar çiftleri biçiminde depolamak.
var sp = SharedPreference(this);
Veri depolama:
String, Int ve Boolean verilerini depolamak için aynı ada ve farklı parametrelere sahip üç yöntemimiz var (Yöntem aşırı yükleme).
save("key-name1","string value")
save("key-name2",int value)
save("key-name3",boolean)
Verileri Al: SharedPreferences'da depolanan verileri almak için aşağıdaki yöntemleri kullanın.
sp.getValueString("user_name")
sp.getValueInt("user_id")
sp.getValueBoolean("user_session",true)
Tüm Verileri Temizle: Tüm Paylaşılan Tercihleri temizlemek için aşağıdaki kodu kullanın.
sp.clearSharedPreference()
Belirli Verileri Kaldır:
sp.removeValue("user_name")
Ortak Paylaşılan Tercih Sınıfı
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