Android State Management
Priority: P0
1. Structure the ViewModel
- Expose ONE
StateFlow<UiState>via.asStateFlow(). - Use
viewModelScopefor all coroutines. - Trigger initial load in
initblock.
See templates for ViewModel and UiState examples.
2. Define UI State (LCE Pattern)
- Use sealed interface with Loading, Content, Error variants.
- Data classes inside should be
@Immutable.
See templates for sealed UiState pattern.
3. Collect State Lifecycle-Safely
- Use
collectAsStateWithLifecycle()in Compose. - Use
SharingStarted.WhileSubscribed(5000)for shared resources.
Anti-Patterns
- No LiveData for New Code: Use StateFlow — lifecycle-safe and Compose-compatible.
- No Public MutableStateFlow: Expose only
.asStateFlow()to consumers. - No Context in ViewModel: Leaks Activity. Use Application context if truly needed.