back
loading skill details...
Best practices for implementing Android ViewModels, specifically focused on StateFlow for UI state and SharedFlow for one-off events.
Android ViewModel & State Management
Instructions
Use ViewModel to hold state and business logic. It must outlive configuration changes.
1. UI State (StateFlow)
What: Represents the persistent state of the UI (e.g., Loading, Success(data), Error).
Type: StateFlow<UiState>.
Initialization: Must have an initial value.
Exposure: Expose as a read-only StateFlow backing a private MutableStateFlow.
private val _uiState = MutableStateFlow<UiState>(UiState.Loading)
val uiState: StateFlow<UiState> = _uiState.asStateFlow()
Updates: Update state using .update { oldState -> ... } for thread safety.don't have the plugin yet? install it then click "run inline in claude" again.