Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
new-silvermoon avatar

Android Viewmodel

  • 451 installs
  • 910 repo stars
  • Updated July 27, 2026
  • new-silvermoon/awesome-android-agent-skills

android-viewmodel is an agent skill that manages Android UI state with ViewModel patterns using StateFlow for persistent UI state and SharedFlow for one-off events so configuration changes do not destroy business logic i

About

android-viewmodel is an agent skill from new-silvermoon/awesome-android-agent-skills that codifies Android ViewModel best practices with Kotlin coroutines flows. The skill uses StateFlow with an initial value and a private MutableStateFlow backing store for persistent UI states like Loading, Success, and Error. SharedFlow handles one-off events such as navigation or toasts while ViewModels outlive configuration changes. Android developers reach for android-viewmodel when agents generate Activities or Fragments that leak business logic or lose state on rotation.

  • Lifecycle-aware UI state
  • Rotation-safe data holders
  • UI event to action mapping
  • Testable presentation logic
  • Compose and XML compatibility

Android Viewmodel by the numbers

  • 451 all-time installs (skills.sh)
  • +17 installs in the week ending Aug 4, 2026 (Skillselion tracking)
  • Ranked #317 of 1,039 Mobile Development skills by installs in the Skillselion catalog
  • Data as of Aug 4, 2026 (Skillselion catalog sync)
npx skills add https://github.com/new-silvermoon/awesome-android-agent-skills --skill android-viewmodel

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs451
repo stars910
Last updatedJuly 27, 2026
Repositorynew-silvermoon/awesome-android-agent-skills

How do you manage Android UI state with ViewModel?

Manage Android UI state with ViewModel patterns that survive rotation, expose observable state, and keep business logic out of Activities.

Who is it for?

Android developers using Kotlin who need rotation-safe UI state with StateFlow and one-off SharedFlow events outside Activities.

Skip if: Compose-only projects using unidirectional data flow without ViewModel or legacy Java Activities without coroutines flow support.

When should I use this skill?

A developer asks to add an Android ViewModel, expose StateFlow UI state, or handle one-off events with SharedFlow.

What you get

Kotlin ViewModel class with StateFlow UI state, SharedFlow events, and Activity-free business logic.

  • ViewModel class
  • UiState sealed hierarchy

Files

SKILL.mdMarkdownGitHub ↗

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.

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.
    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.
    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.

Related skills

FAQ

When should android-viewmodel use StateFlow versus SharedFlow?

android-viewmodel uses StateFlow for persistent UI state such as Loading, Success, or Error with a required initial value. SharedFlow carries one-off events like navigation or toasts that should not be replayed as state.

How does android-viewmodel handle configuration changes?

android-viewmodel places state and business logic in a ViewModel that outlives Activity recreation. StateFlow exposes read-only UI state while a private MutableStateFlow holds mutable updates safely.

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.