Android KTX

Yuree Choiยท2022๋…„ 6์›” 13์ผ
0

Android KTX

๐Ÿ”— Android KTX ๊ณต์‹๋ฌธ์„œ LINK

1. ๊ฐœ๋…

Android KTX๋Š” ์ฝ”๋“œ๋ฅผ ๋” ๊ฐ„๊ฒฐํ•˜๊ณ  ์ง๊ด€์ ์œผ๋กœ ์งค ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ํ™•์žฅ ํ”„๋กœ๊ทธ๋žจ๊ณผ ๊ฐ™๋‹ค.

2. ์˜ˆ์‹œ

๊ณต์‹๋ฌธ์„œ์—์„œ ์˜ˆ๋ฅผ ๋“ค๊ณ  ์žˆ๋Š”๊ฒŒ ๋ฐ”๋กœ ๋‚ด๊ฐ€ ์ผ๋˜ sharedPreferences!

sharedPreferences
        .edit()  // create an Editor
        .putBoolean("key", value)
        .apply() // write to disk asynchronously

์ด ์ฝ”๋“œ๋ฅผ

sharedPreferences.edit(commit = true) { putBoolean("key", value) }

์ด๋ ‡๊ฒŒ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ค„์ผ ์ˆ˜๊ฐ€ ์žˆ๋‹ค..!

3. SOPT ๊ณผ์ œ์— ์ ์šฉ

๊ณต์‹๋ฌธ์„œ์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ LoginSharedPreferences์— ์ ์šฉํ•ด์คฌ๋‹ค

fun setAutoLogin(context: Context, value: Boolean) {
		 preferences.edit()
    	.putBoolean(AUTO_LOGIN, value)
    	.apply()
    }

์—ฌ๋Ÿฌ ์ค„์ด์—ˆ๋˜ edit, apply ๊ณผ์ •์„

fun setAutoLogin(context: Context, value: Boolean) {
        preferences.edit(commit = true) { putBoolean("key", value) } 
        //Android KTX ์‚ฌ์šฉ
    }

์ด๋ ‡๊ฒŒ ์ค„์ผ ์ˆ˜๊ฐ€ ์žˆ๋‹ค ~!

profile
๋งˆ์Œ๋งŒ์€ ์ž”๋””๋ฐญ

0๊ฐœ์˜ ๋Œ“๊ธ€