#1 ๊ฐ์
์ํ๋ฅผ ํธ์ด์คํ ํ ๋์ ์์น | Jetpack Compose | Android Developers
์ด ํ์ด์ง๋ Cloud Translation API๋ฅผ ํตํด ๋ฒ์ญ๋์์ต๋๋ค. ์ํ๋ฅผ ํธ์ด์คํ ํ ๋์ ์์น ์ปฌ๋ ์ ์ ์ฌ์ฉํด ์ ๋ฆฌํ๊ธฐ ๋ด ํ๊ฒฝ์ค์ ์ ๊ธฐ์ค์ผ๋ก ์ฝํ ์ธ ๋ฅผ ์ ์ฅํ๊ณ ๋ถ๋ฅํ์ธ์. Compose ์ ํ๋ฆฌ์ผ์ด์ ์์
developer.android.com
State Hoisting(์ํ ํธ์ด์คํ
) ํจํด์ ์ ์ฉํด ์ฝ๋์ ์ ์ฌ์ ์ ์ง๋ณด์์ฑ์ ๋ํ๋ณธ๋ค.
#2 ์ฝ๋
#2-1 State Hoisting ํจํด์ด ์ ์ฉ๋์ง ์์ ์ฝ๋
...
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Box(
modifier = Modifier.fillMaxSize()
) {
ButtonExample(Modifier.align(Alignment.Center))
}
}
}
}
// โ โ โ Stateful Composable
@Composable
fun ButtonExample(modifierParam: Modifier = Modifier) {
val count = remember {
mutableStateOf(0)
}
Button(
onClick = { count.value++ },
modifier = modifierParam
) {
Text(
text = "Count: ${count.value}",
fontSize = 40.sp
)
}
}
์ ์ฝ๋์ ButtonExample()์ฒ๋ผ State๋ฅผ ๋ณด์ ํ Composable์ Stateful Composable์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค. Stateful Composable์ ์ฌ์ฌ์ฉ์ฑ์ด ๋จ์ด์ง๊ณ ํ
์คํธํ๊ธฐ๋ ์ด๋ ต๋ค๋ ๋ฌธ์ ๋ฅผ ๊ฐ์ง๋ค. ์๋ํ๋ฉด, ์์กด์ฑ ์ฃผ์
์ ๊ตฌํํ์ง ์์๊ธฐ ๋๋ฌธ์ด๋ค. ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ค๋ฉด State๋ฅผ ์ธ๋ถ ์์ฑ์๋ก๋ถํฐ ์ฃผ์
๋ฐ๋ ๋ฐฉ์์ผ๋ก, ์ฆ Composable์ด ์์ฑ์ ์ฃผ์
(Constructor Injection)์ ๊ตฌํํ๊ฒ๋ ๋ณ๊ฒฝํด์ผ ํ๋ค.
#2-2 State Hoisting ํจํด์ด ์ ์ฉ๋ ์ฝ๋
...
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Box(
modifier = Modifier.fillMaxSize(),
) {
val count = remember {
mutableStateOf(0)
}
ButtonExample(
count.value,
Modifier.align(Alignment.Center)
) { newValue ->
count.value = newValue + 1
}
}
}
}
}
// โ โ โ Stateless Composable
@Composable
fun ButtonExample(
currentCount: Int,
modifierParam: Modifier = Modifier,
updateCount: (Int) -> Unit,
) {
Button(
onClick = { updateCount(currentCount) }, // updateCount()๋ ButtonExample()์ด ์๋, ButtonExample()๋ฅผ ํธ์ถํ ์์ Composable์์ ์ฒ๋ฆฌ๋๋ค.
modifier = modifierParam
) {
Text(
text = "Count: $currentCount",
fontSize = 40.sp
)
}
}
State๊ฐ ์๋ Composable์ด Stateful Composable์ด์๋ ๊ฒ์ฒ๋ผ, State๊ฐ ์๋ Composable์ Stateless Composable๋ผ๊ณ ๋ถ๋ฅธ๋ค. State๋ฅผ ์์ฑ์ ์ฃผ์
๋ฐ๋ ๊ฒฝ์ฐ๋ Stateless Composable์ด๋ค. ์ฐ์ State๋ฅผ setContent { ... }๋ก ์ด๋์ํค๊ณ ButtonExample์ ๋งค๊ฐ๋ณ์์ State์ ์ ๋ค๋ฆญ ํ์
(์ฌ๊ธฐ์๋ Int)์ธ currentCount๋ฅผ ์ถ๊ฐํ๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฒํผ์ ํด๋ฆญ ๋ฆฌ์ค๋์ ๋ฃ์ ๋๋ค ํจ์๋ ๋งค๊ฐ๋ณ์๋ก ์ถ๊ฐํ๋ค. ์ด ๋๋ค ํจ์๋ setContent { ... }์ ์๋ State์ ๊ฐ์ ์กฐ์ํ๋ค.
์ด๋ ๊ฒ ๋ง๋ค์ด์ง ์ ์ฝ๋์ ๊ตฌ์กฐ๋ฅผ State Hoisting ํจํด์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค. hoist์ ์ฌ์ ์ ์๋ฏธ๋ (ํํ ๋ฐง์ค์ด๋ ์ฅ๋น๋ฅผ ์ด์ฉํ์ฌ) ๋ค์ด[๋์ด]์ฌ๋ฆฌ๋ค์ธ๋ฐ, State๋ฅผ ํ์ Composable (๋ณธ ๊ฒ์๊ธ์์๋ ButtonExample())๋ก๋ถํฐ ์์ Composable (๋ณธ ๊ฒ์๊ธ์์๋ setContent)๊น์ง ๋์ด ๋น๊ธด๋ค๋ ๋ฌ์์ค๋ก ์ฐ์ธ ๊ฒ์ด๋ค.
#3 State Hoisting ํจํด์ ์์
State Hoisting์ ๋จ๋ฐฉํฅ ๋ฐ์ดํฐ ํ๋ฆ(Unidirectional Data Flow)์ ๊ตฌ์ถํ๋ ๋ฐ ์ฃผ์ํ๊ฒ ์ฌ์ฉ๋๋ ํจํด์ด๋ค. ๋จ๋ฐฉํฅ ๋ฐ์ดํฐ ํ๋ฆ์ด๋, ์ํ(State)๋ ์(setContent)์์ ์๋(ButtonExample())๋ก ๋ณด๋ด๊ณ , ์ด๋ฒคํธ(ํด๋ฆญ ์ด๋ฒคํธ ๋ฑ)์ ์๋(ButtomExample())์์ ์(setContent)๋ก ๋ณด๋ด๋ ํ๋ฆ์ ์๋ฏธํ๋ค. ๋ฐ๋ผ์ State Hoisting ํจํด์ ์ ์ฉํ Composable์๊ฒ๋ 2๊ฐ์ง๊ฐ ์๊ตฌ๋๋ค. Composable์ด ํ์ฌ ํ์ํ ๊ฐ๊ณผ ๊ทธ ๊ฐ์ ๋ณ๊ฒฝ ์์ฒญํ๋ ์ด๋ฒคํธ๋ค. ์ ์๋ #2-2์ currentCount์ ํด๋นํ๊ณ ํ์๋ #2-2์ updateCount์ ํด๋นํ๋ค.
๋จ๋ฐฉํฅ ๋ฐ์ดํฐ ํ๋ฆ์ด ๊ตฌ์ถ๋๋ฉด, ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ์๋ฒ๋ก๋ถํฐ State๋ฅผ ๋ฐ์์ Staless Composable์ ์ฃผ์
์์ผ์ค ์ ์๋ค๋ ์ ์์ ์บก์ํ ๋ฐ ๋ชจ๋ํ๊ฐ ๋ฌ์ฑ๋๋ค. ๋ State๊ฐ ์์ Composable์ ์์นํ๊ธฐ์ ๋ ์ค์์ง์ค์ ์ธ ์์น์์ State๋ฅผ ์ผ๊ด์ ์ผ๋ก, ์ฝ๊ฒ ๊ด๋ฆฌํ ์ ์๊ฒ ๋๋ค.
#4 ์์ฝ
State Hoisting์ State๋ฅผ ์์ Composable๋ก ์ฎ๊ธฐ๋ ์ฝ๋ ํจํด์ด๋ค.
#5 ์์ฑ๋ ์ฑ
android-practice/jetpack-compose/StateHoisting at master ยท Kanmanemone/android-practice
Contribute to Kanmanemone/android-practice development by creating an account on GitHub.
github.com
#6 ์ด์ด์ง๋ ๊ธ
[Android] Jetpack Compose - ViewModel์์ State ์ฌ์ฉํ๊ธฐ
#1 ๊ฐ์Jetpack Compose์์ ViewModel์ ์ฌ์ฉํด๋ณธ๋ค. Jetpack Compose๋ฅผ ์ฌ์ฉํ์ง ์๋ ์ ํต์ ์ธ ๋ฐฉ์์์์ ViewModel๊ณผ ํฌ๊ฒ ๋ค๋ฅผ ๊ฒ ์๋ค. Jetpack Compose์ ViewModel์ ๊ตฌํํจ์ผ๋ก์จ State Hoisting ํจํด์ ๊ทน๋ํ์
kenel.tistory.com
ViewModel์ ์ด์ฉํจ์ผ๋ก์จ, State Hoisting ํจํด์ ์ฆ๋์ํจ๋ค.
'๊นจ์ ๊ฐ๋ ๐ > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] Coroutines Flow - Jetpack Compose์์ ์ฌ์ฉํ๊ธฐ (1) | 2024.08.06 |
---|---|
[Android] Jetpack Compose - ViewModel์์ State ์ฌ์ฉํ๊ธฐ (0) | 2024.07.23 |
[Android] Jetpack Compose - State Remembering (0) | 2024.07.23 |
[Android] Jetpack Compose - State ๊ธฐ์ด (0) | 2024.07.22 |
[Android] Jetpack Compose - Lazy Column (0) | 2024.07.22 |