Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Kotlin - SharedPreferences
package com.codingwithsara.sharedpreftest
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.preference.PreferenceManager
import android.view.Gravity
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)
val pref = PreferenceManager.getDefaultSharedPreferences(this)
pref.apply {
val name = getString("NAME", "")
val email = getString("EMAIL", "")
editName.setText(name)
editEmail.setText(email)
}
}
fun saveData(v: View) {
val pref = PreferenceManager.getDefaultSharedPreferences(this)
val editor = pref.edit()
editor
.putString("NAME", editName.text.toString())
.putString("EMAIL", editEmail.text.toString())
.apply()
val toast = Toast.makeText(applicationContext, "Saved", Toast.LENGTH_LONG)
toast.setGravity(Gravity.TOP, 0, 140)
toast.show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.