smithery/shetiejun

android-viewmodel

Best practices for implementing Android ViewModels, specifically focused on StateFlow for UI state and SharedFlow for one-off events.

Installation

$ npx skills add smithery/shetiejun --skill android-viewmodel

Similar popular skills

Related neighbors and high-traction skills in the same topics — useful to compare before installing.

Also in this package

Other skills from smithery/shetiejun.

npx skills add smithery/shetiejun

Browse all from smithery/shetiejun

More details

Agent compatibility

Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.

Claude Code Not declared
Cursor Not declared
Codex Not declared
GitHub Copilot Not declared
Windsurf Not declared
Gemini CLI Not declared
Cline Not declared
OpenCode Not declared

Package contents

Files included with this skill beyond the listing page.

  • skill md SKILL.md 1,863 B
  • docs SUMMARY.md 158 B

History

  1. First recorded snapshot · 0 installs

SKILL.md

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.

``kotlin private val uiState = MutableStateFlow<UiState>(UiState.Loading) val uiState: StateFlow<UiState> = uiState.asStateFlow() ``

  • Updates: Update state using .update { oldState -> ... } for thread safety.

2. One-Off Events (SharedFlow)

  • What: Transient events like "Show Toast", "Navigate to Screen", "Show Snackbar".
  • Type: SharedFlow<UiEvent>.
  • Configuration: Must use replay = 0 to prevent events from re-triggering on screen rotation.

``kotlin private val uiEvent = MutableSharedFlow<UiEvent>(replay = 0) val uiEvent: SharedFlow<UiEvent> = uiEvent.asSharedFlow() ``

  • Sending: Use .emit(event) (suspend) or .tryEmit(event).

3. Collecting in UI

  • Compose: Use collectAsStateWithLifecycle() for StateFlow.

``kotlin val state by viewModel.uiState.collectAsStateWithLifecycle() ` For SharedFlow, use LaunchedEffect with LocalLifecycleOwner`.

  • Views (XML): Use repeatOnLifecycle(Lifecycle.State.STARTED) within a coroutine.

4. Scope

  • Use viewModelScope for all coroutines started by the ViewModel.
  • Ideally, specific operations should be delegated to UseCases or Repositories.