[G1-Sync] Manual knowledge update
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
---
|
||||
id: android-viewmodel-state-persistence
|
||||
title: ViewModel + SavedStateHandle — 상태 보존
|
||||
category: Coding
|
||||
status: draft
|
||||
source_trust_level: B
|
||||
verification_status: conceptual
|
||||
created_at: 2026-05-09
|
||||
updated_at: 2026-05-09
|
||||
tags: [android, viewmodel, saved-state, vibe-coding]
|
||||
tech_stack: { language: "Kotlin / Jetpack ViewModel", applicable_to: ["Android"] }
|
||||
applied_in: []
|
||||
aliases: [SavedStateHandle, process death, configuration change]
|
||||
---
|
||||
|
||||
# ViewModel + SavedStateHandle
|
||||
|
||||
> ViewModel 은 **configuration change** (회전) 살아남음. 그러나 **process death** (시스템 메모리 부족 → 앱 죽임) 에는 살아남지 못함. 둘 다 보존하려면 SavedStateHandle.
|
||||
|
||||
## 📖 핵심 개념
|
||||
- ViewModel.onCleared(): user 가 진짜 화면 닫음.
|
||||
- Configuration change: 회전, 다크모드, 언어 — ViewModel 살아남음.
|
||||
- Process death: 메모리 부족 시 OS 가 앱 process kill — ViewModel 도 사라짐. 사용자가 돌아오면 새 ViewModel.
|
||||
- SavedStateHandle: Bundle 에 자동 저장 + 복원.
|
||||
|
||||
## 💻 코드 패턴
|
||||
|
||||
### 기본 — SavedStateHandle 주입
|
||||
```kotlin
|
||||
class SearchViewModel @AssistedInject constructor(
|
||||
@Assisted private val savedState: SavedStateHandle,
|
||||
private val repo: SearchRepository,
|
||||
) : ViewModel() {
|
||||
// 자동 저장/복원
|
||||
val query: StateFlow<String> = savedState.getStateFlow("query", "")
|
||||
fun setQuery(s: String) { savedState["query"] = s }
|
||||
|
||||
val results: StateFlow<List<Item>> = query
|
||||
.debounce(300)
|
||||
.flatMapLatest { repo.search(it) }
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
|
||||
}
|
||||
```
|
||||
|
||||
### Hilt 주입 (간단)
|
||||
```kotlin
|
||||
@HiltViewModel
|
||||
class SearchViewModel @Inject constructor(
|
||||
private val savedState: SavedStateHandle,
|
||||
private val repo: SearchRepository,
|
||||
) : ViewModel() { ... }
|
||||
```
|
||||
|
||||
### Compose
|
||||
```kotlin
|
||||
@Composable
|
||||
fun SearchScreen(viewModel: SearchViewModel = hiltViewModel()) {
|
||||
val query by viewModel.query.collectAsStateWithLifecycle()
|
||||
val results by viewModel.results.collectAsStateWithLifecycle()
|
||||
Column {
|
||||
TextField(value = query, onValueChange = viewModel::setQuery)
|
||||
LazyColumn { items(results) { ... } }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Navigation arguments 도 SavedStateHandle 로
|
||||
```kotlin
|
||||
class DetailViewModel(savedState: SavedStateHandle) : ViewModel() {
|
||||
val id: String = savedState["userId"] ?: error("missing userId")
|
||||
}
|
||||
```
|
||||
|
||||
## 🤔 의사결정 기준
|
||||
| 데이터 | 보존 |
|
||||
|---|---|
|
||||
| 큰 list (서버 fetch) | 보존 X — 다시 fetch (캐시는 repository) |
|
||||
| 사용자 입력 (아직 제출 안 됨) | SavedStateHandle |
|
||||
| 화면 작은 toggle, 펼침 | rememberSaveable |
|
||||
| Auth token | EncryptedSharedPreferences / DataStore (ViewModel 부적합) |
|
||||
| Navigation args | SavedStateHandle |
|
||||
|
||||
## ❌ 안티패턴
|
||||
- **모든 state 를 SavedStateHandle 에**: Bundle 한계 (~500KB) + serialization 비용. 진짜 입력 / 식별자만.
|
||||
- **ViewModel 에 Context 직접 보유**: leak. AndroidViewModel 또는 Hilt @ApplicationContext.
|
||||
- **viewModelScope 안에서 LiveData / StateFlow 동시 사용**: 일관성. 하나로.
|
||||
- **process death 후 stateFlow 의 collect 가 stale 값 emit**: 새 VM 인스턴스라서 정상이지만, transient state 보존 필요하면 SavedStateHandle.
|
||||
- **navigation args 를 ViewModel constructor 에 직접**: 못 함. SavedStateHandle 통해.
|
||||
|
||||
## 🤖 LLM 활용 힌트
|
||||
- "사용자 입력 / 식별자는 SavedStateHandle, 서버 데이터는 repository 캐시" 분리.
|
||||
- StateFlow + WhileSubscribed(5000) 가 표준 SharingStarted.
|
||||
|
||||
## 🔗 관련 문서
|
||||
- [[Android_Lifecycle_Aware_Components]]
|
||||
- [[Android_Compose_State_Hoisting]]
|
||||
Reference in New Issue
Block a user