반응형
SharedPreferences
설명
- 앱 내부에 간단한 데이터를 저장하여 사용할 때 사용
코드
MainActivityclass MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater).apply {
setContentView(root)
lifecycleOwner = this@MainActivity
}
}
fun onClickSave(view: View) {
val pref = getSharedPreferences("settings", 0)
val edit = pref.edit()
edit.putString("userName", "john")
edit.apply()
}
fun onClickShow(view: View) {
val pref = getSharedPreferences("settings", 0)
Toast.makeText(this, pref.getString("userName", "Unknown"), Toast.LENGTH_SHORT).show()
}
}
참고
반응형
'Development > Android' 카테고리의 다른 글
[Android] meta-data (0) | 2021.02.09 |
---|---|
[Android] Kotpref (0) | 2021.02.09 |
[Android] Build APK (0) | 2021.02.09 |
[Android] Retrofit (0) | 2021.02.09 |
[Android] Lottie (0) | 2021.02.09 |