Preferensi bersama umum (CURD) SharedPreference: untuk Menyimpan data dalam bentuk pasangan nilai-kunci dengan kelas Kotlin sederhana.
var sp = SharedPreference(this);
Menyimpan Data:
Untuk menyimpan data String, Int dan Boolean kami memiliki tiga metode dengan nama yang sama dan parameter berbeda (Metode overloading).
save("key-name1","string value")
save("key-name2",int value)
save("key-name3",boolean)
Ambil Data: Untuk Mengambil data yang disimpan di SharedPreferences gunakan metode berikut.
sp.getValueString("user_name")
sp.getValueInt("user_id")
sp.getValueBoolean("user_session",true)
Hapus Semua Data: Untuk menghapus seluruh SharedPreferences gunakan kode di bawah ini.
sp.clearSharedPreference()
Hapus Data Tertentu:
sp.removeValue("user_name")
Kelas Preferensi Bersama Umum
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