์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- ์ฝํ๋ฆฐ
- MVP
- ์๋๋ก์ด๋ ์๋ช ์ฃผ๊ธฐ
- ์๋๋ก์ด๋
- andoid
- AAC
- android recyclerview
- ์๋๋ก์ด๋ ์๋๋ก๊ทธ์ธ
- ์๋๋ก์ด๋ RecyclerView
- ViewPager
- MVVM
- android SharedPreferences
- android clipToPadding
- ๋ฆฌ์ฌ์ดํด๋ฌ๋ทฐ
- android startActivityForResult
- activity
- Android
- MVC
- kotlin
- SharedPreferences
- fragment
- ์๋๋ก์ด๋ Bottom Navigation
- Navigation
- Bottom Navigation
- android itemDecoration
- ์๋ช ์ฃผ๊ธฐ
- ์๋๋ก์ด๋ ViewPager
- recyclerview
- Today
- Total
my repository
Android :: SharedPreferences๋ฅผ ์ด์ฉํ ์๋ ๋ก๊ทธ์ธ ๊ธฐ๋ฅ ๋ณธ๋ฌธ
Android :: SharedPreferences๋ฅผ ์ด์ฉํ ์๋ ๋ก๊ทธ์ธ ๊ธฐ๋ฅ
hjin 2020. 6. 10. 17:29๐ก SharedPreferences
SharedPreferences๋ (key, value) ํํ๋ก ๊ด๋ฆฌ๋๋ฉฐ, ๊ฐ๋จํ ๊ฐ ์ ์ฅ์ ์ฌ์ฉํ๊ธฐ ์ข๋ค.
๋ฐ์ดํฐ์ ์์ด ๋ง๋ค๋ฉด ์๋ฒ๋ DB์ ํํ๋ก ์ ์ฅ์ ํ๋ ๊ฒ์ด ์ข์ง๋ง, ์ด๊ธฐ ์ค์ ๊ฐ์ด๋ ์๋ ๋ก๊ทธ์ธ๊ณผ ๊ฐ์ด ๊ฐ๋จํ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํด์ผ ํ๋ ๊ฒฝ์ฐ์๋ SharedPreferences๋ฅผ ์ฌ์ฉํ์ฌ ์ฝ๊ฒ ๊ด๋ฆฌํ ์ ์๋ค.
1. SharedPreferences ์์ฑ
val prefs : SharedPreferences = context.getSharedPreferences("Prefs", Context.MODE_PRIVATE)
val editor : SharedPreferences.Editor = prefs.edit() // ๋ฐ์ดํฐ ๊ธฐ๋ก์ ์ํ editor
(1) getSharedPreferences(key, mode)
MODE_PRIVATE : ์๊ธฐ ์ฑ๋ด์์๋ง ์ฌ์ฉํ ๋ (0์ ์ ๋ ฅํด๋ ๋๋ค)
MODE_WORLD_READABLE : ๋ค๋ฅธ ์ฑ์์ ์ฝ๊ธฐ ๊ฐ๋ฅ
MODE_WORLD_WRITEABLE : ๋ค๋ฅธ ์ฑ์์ ์ฐ๊ธฐ ๊ฐ๋ฅ
(2) ๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋กํ๊ธฐ ์ํด์๋ SharedPreferences.Editor ์ธ์คํด์ค๋ฅผ ์ป์ด์ผํ๋ค.
์์ฑํ editor๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ์ ์ ์ฅ, ์์ , ์ญ์ ๊ฐ ๊ฐ๋ฅํ๋ค.
2. ๋ฐ์ดํฐ ์ ์ฅ
editor.putString(key, value)
editor.commit() // ํ์
Editor ๊ฐ์ฒด๋ฅผ ํตํด ์ํ๋ ๊ฐ์ key, value ํํ๋ก ์ ๋ ฅํ๊ณ
commit ํด์ฃผ๋ฉด ๋ฐ์ดํฐ ์ ์ฅ์ด ์๋ฃ๋๋ค.
์ฌ๊ธฐ์ ์ค์ํ ์ ์ ๊ฐ์ ๋ฃ์ด์ฃผ๊ณ ๋์ commit์ ๊ผญ !! ํด์ฃผ์ด์ผ ํ๋ค๋ ์ ์ด๋ค.
์ ์ฅํ ์ ์๋ ๋ฐ์ดํฐ ์ข ๋ฅ๋ boolean, int, float, long, string์ด ์๋ค.
3. ์ ์ฅ๋ ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
prefs.getString(key, "")
SharedPreferences ๊ฐ์ฒด prefs๋ฅผ ํตํด ์ํ๋ ๊ฐ์ key์ defaultValue(key ๊ฐ์ด null์ผ ๊ฒฝ์ฐ ๋ฐํํ ๊ฐ)๋ฅผ
ํ๋ผ๋ฏธํฐ๋ก ํ์ฌ ํธ์ถํ๋ฉด ๊ฐ์ ๋ฐ์์ฌ ์ ์๋ค.
4. ๋ชจ๋ ๋ฐ์ดํฐ ์ญ์
editor.remove(key)
editor.clear()
editor.commit()
remove(key)๋ฅผ ์ฌ์ฉํ๋ฉด key์ ํด๋นํ๋ ๋ฐ์ดํฐ๊ฐ ์ญ์ ๋๊ณ ,
clear()๋ฅผ ์ฌ์ฉํ๋ฉด SharedPreferences์ ๋ชจ๋ ๋ฐ์ดํฐ๊ฐ ์ญ์ ๋๋ค.
๋ง์ฐฌ๊ฐ์ง๋ก ๋ณ๊ฒฝ ํ์๋ commit์ ํด์ค๋ค.
๐ ์์
๋ก๊ทธ์ธ ํ ์ฑ์ ์ข ๋ฃํ๋ค๊ฐ ์ฌ์์ ์ ์๋ ๋ก๊ทธ์ธ ๋๋๋ก ๊ตฌํ
< MySharedPreferences.kt >
object MySharedPreferences {
private val MY_ACCOUNT : String = "account"
fun setUserId(context: Context, input: String) {
val prefs : SharedPreferences = context.getSharedPreferences(MY_ACCOUNT, Context.MODE_PRIVATE)
val editor : SharedPreferences.Editor = prefs.edit()
editor.putString("MY_ID", input)
editor.commit()
}
fun getUserId(context: Context): String {
val prefs : SharedPreferences = context.getSharedPreferences(MY_ACCOUNT, Context.MODE_PRIVATE)
return prefs.getString("MY_ID", "").toString()
}
fun setUserPass(context: Context, input: String) {
val prefs : SharedPreferences = context.getSharedPreferences(MY_ACCOUNT, Context.MODE_PRIVATE)
val editor : SharedPreferences.Editor = prefs.edit()
editor.putString("MY_PASS", input)
editor.commit()
}
fun getUserPass(context: Context): String {
val prefs : SharedPreferences = context.getSharedPreferences(MY_ACCOUNT, Context.MODE_PRIVATE)
return prefs.getString("MY_PASS", "").toString()
}
fun clearUser(context: Context) {
val prefs : SharedPreferences = context.getSharedPreferences(MY_ACCOUNT, Context.MODE_PRIVATE)
val editor : SharedPreferences.Editor = prefs.edit()
editor.clear()
editor.commit()
}
}
< LoginActivity.kt >
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
// SharedPreferences ์์ ๊ฐ์ด ์ ์ฅ๋์ด ์์ง ์์ ๋ -> Login
if(MySharedPreferences.getUserId(this).isNullOrBlank()
|| MySharedPreferences.getUserPass(this).isNullOrBlank()) {
Login()
}
else { // SharedPreferences ์์ ๊ฐ์ด ์ ์ฅ๋์ด ์์ ๋ -> MainActivity๋ก ์ด๋
Toast.makeText(this, "${MySharedPreferences.getUserId(this)}๋
์๋ ๋ก๊ทธ์ธ ๋์์ต๋๋ค.", Toast.LENGTH_SHORT).show()
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
}
fun Login() {
btn_login.setOnClickListener {
if(et_id.text.isNullOrBlank() || et_pass.text.isNullOrBlank()) {
Toast.makeText(this, "์์ด๋์ ๋น๋ฐ๋ฒํธ๋ฅผ ํ์ธํ์ธ์", Toast.LENGTH_SHORT).show()
}
else {
MySharedPreferences.setUserId(this, et_id.text.toString())
MySharedPreferences.setUserPass(this, et_pass.text.toString())
Toast.makeText(this, "${MySharedPreferences.getUserId(this)}๋
๋ก๊ทธ์ธ ๋์์ต๋๋ค.", Toast.LENGTH_SHORT).show()
var intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
}
}
}
< MainActivity.kt >
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv_info.setText("ID : " + MySharedPreferences.getUserId(this))
btn_logout.setOnClickListener {
MySharedPreferences.clearUser(this)
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
finish()
}
}
}
๐ป ๊ฒฐ๊ณผ
1. ์์ด๋ ํจ์ค์๋ ์ ๋ ฅ → ๋ก๊ทธ์ธ
2. ๋ก๊ทธ์ธ ๋ ํ๋ฉด
3. ์ฑ ์ข ๋ฃ ํ ์ฌ์์ → ์๋ ๋ก๊ทธ์ธ
Android ๊ณต์๋ฌธ์ : https://developer.android.com/training/data-storage/shared-preferences?hl=ko
'IT > Android (Kotlin)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Android :: Activity, Fragment ์๋ช ์ฃผ๊ธฐ (0) | 2022.03.01 |
---|---|
Android :: JetPack - AAC Navigation Component ์ฌ์ฉ๋ฒ (0) | 2022.02.22 |
Android :: RecyclerView ๋ฆฌ์ฌ์ดํด๋ฌ๋ทฐ ์ฌ์ฉ๋ฒ (0) | 2020.06.12 |
Android :: Bottom Navigation์ ViewPager ์ฐ๊ฒฐ (0) | 2020.06.10 |
Android :: startActivityForResult ์กํฐ๋นํฐ ๊ฐ ๋ฐ์ดํฐ ์ ๋ฌ (0) | 2020.06.10 |