Kotlin ile SharedPreferences Kullanımı

Uygulamalarımız içinde küçük verileri kayıt etmek için veritabanı oluşturmak yerine kullanımı daha pratik olan SharedPreferences kullanabiliriz ama büyük veriler için kullanımı uygun değildir. Basit bir uygulama yaparak SharedPreferences ile nasıl veri ekleme, güncelleme, silme işlemelerini yapabileceğimize bakalım.
Uygulamamızın genel mantığı kullanıcı sayfada ismini ve yaşını yazacak göster butonuna bastığı zaman ismini ve yaşını göstereceğiz. Bu veriler uygulama kapansa bile kayıt edildiğini yukardaki gife bakarak anlayabilirsiniz.
İlk olarak bir proje oluşturuyoruz MainActivity.java sınıfını ve activity_main.xml dosyamızı aşağıdaki gibi düzenliyoruz.
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.esatgozcu.kotlinsharedpreferences.MainActivity" tools:layout_editor_absoluteY="81dp"> <EditText android:id="@+id/nameText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="40dp" android:ems="10" android:inputType="textPersonName" app:layout_constraintBottom_toTopOf="@+id/ageText" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> <EditText android:id="@+id/ageText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="33dp" android:ems="10" android:inputType="textPersonName" app:layout_constraintBottom_toTopOf="@+id/addButton" app:layout_constraintHorizontal_bias="0.502" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> <Button android:id="@+id/addButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="27dp" android:onClick="add" android:text="Ekle" app:layout_constraintBottom_toTopOf="@+id/showButton" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> <Button android:id="@+id/showButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="30dp" android:onClick="show" android:text="GÖSTER" app:layout_constraintBottom_toTopOf="@+id/removeButton" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> <Button android:id="@+id/removeButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="48dp" android:onClick="remove" android:text="SİL" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> </android.support.constraint.ConstraintLayout> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package com.example.esatgozcu.kotlinsharedpreferences import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Toast import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } // Ekle butonuna basılınca.. fun add(view: View) { // sharedPreferences nesnemizi oluşturuyoruz. val sharedPreferences = this.getSharedPreferences(packageName,android.content.Context.MODE_PRIVATE) val name = nameText.text.toString() val age = ageText.text.toString().toInt() // Verimizi name anahtar kelimesi ile ekliyoruz sharedPreferences.edit().putString("name", name).apply() // Verimizi age anahtar kelimesi ile ekliyoruz sharedPreferences.edit().putInt("age",age).apply() // EditTextleri temizliyoruz nameText.setText("") ageText.setText("") } // Göster butonuna basılınca.. fun show(view: View) { val sharedPreferences = this.getSharedPreferences(packageName,android.content.Context.MODE_PRIVATE) // name anahtar kelimesinin verisini çekiyoruz, ikinci parametre default değer val showName = sharedPreferences.getString("name","isimsiz") // age anahtar kelimesinin verisini çekiyoruz, ikinci parametre default değer val showAge = sharedPreferences.getInt("age",0) // Toast mesajını gösteriyoruz Toast.makeText(applicationContext,"İsim : $showName - Yaş : $showAge",Toast.LENGTH_LONG).show() } // Sil butonuna basılınca.. fun remove(view: View) { val sharedPreferences = this.getSharedPreferences(packageName,android.content.Context.MODE_PRIVATE) // Anahtar kelimemize göre verileri kaldırıyoruz sharedPreferences.edit().remove("name").apply() sharedPreferences.edit().remove("age").apply() } } |
Böylelikle SharedPreferences kullanımın nasıl kullanabileceğimizi görmüş olduk.
Projenin kodlarını buraya tıklayarak indirebilirsiniz.