Best practices for implementing Android ViewModels, specifically focused on StateFlow for UI state and SharedFlow for one-off events.
68
60%
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Passed
No known issues
Optimize this skill with Tessl
npx tessl skill review --optimize ./.github/skills/android-viewmodel/SKILL.mdUse ViewModel to hold state and business logic. It must outlive configuration changes.
Loading, Success(data), Error).StateFlow<UiState>.StateFlow backing a private MutableStateFlow.
private val _uiState = MutableStateFlow<UiState>(UiState.Loading)
val uiState: StateFlow<UiState> = _uiState.asStateFlow().update { oldState -> ... } for thread safety.SharedFlow<UiEvent>.replay = 0 to prevent events from re-triggering on screen rotation.
private val _uiEvent = MutableSharedFlow<UiEvent>(replay = 0)
val uiEvent: SharedFlow<UiEvent> = _uiEvent.asSharedFlow().emit(event) (suspend) or .tryEmit(event).collectAsStateWithLifecycle() for StateFlow.
val state by viewModel.uiState.collectAsStateWithLifecycle()SharedFlow, use LaunchedEffect with LocalLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) within a coroutine.viewModelScope for all coroutines started by the ViewModel.3f68e39
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.