This walks you through the standard Android ViewModel pattern with a focus on proper state management using Kotlin flows. You get clear guidance on using StateFlow for persistent UI state like loading and error conditions, and SharedFlow with replay=0 for one-off events like navigation or showing toasts. The replay distinction is the key bit here since it prevents events from re-triggering after configuration changes. It covers the basics like exposing read-only flows, using viewModelScope, and collecting in both Compose and XML views. If you're tired of fighting lifecycle issues or accidentally showing the same snackbar twice after rotation, this nails down the patterns that actually work.

skills.sh installs
576
GitHub stars
1K
last updated
Aug 21, 2026

Install

$ npx -y skills add new-silvermoon/awesome-android-agent-skills --skill android-viewmodel --agent claude-code

Installs into .claude/skills of the current project.

Files

Android ViewModel & State Management

Instructions

Use ViewModel to own UI state and business logic. It should survive configuration changes and expose immutable APIs.

Kotlin 2.3+

Prefer Explicit Backing Fields instead of the traditional _state/state backing property pattern.

  • Kotlin 2.3: Experimental (-Xexplicit-backing-fields)
  • Kotlin 2.4+: Stable (recommended)

Avoid creating duplicate backing properties unless targeting older Kotlin versions.


1. UI State (StateFlow)

Purpose

Represents the persistent UI state.

Examples:

  • Loading
  • Success(data)
  • Error(message)
  • Empty

Declaration

Use an explicit backing field.

val uiState: StateFlow<UiState>
    field = MutableStateFlow(UiState.Loading)

The backing field is mutable (MutableStateFlow) while consumers only see StateFlow.

Updating State

Prefer update { } for atomic updates.

_uiState.update { old ->
    old.copy(isLoading = false)
}

With explicit backing fields:

uiState.update {
    it.copy(isLoading = false)
}

You may also assign directly when appropriate.

Sponsors

Details

Repository
github.com/new-silvermoon/awesome-android-agent-skills
Categories
AI & Agent Building, Mobile Development, Java & JVM
First seen
Jun 3, 2026

More from new-silvermoon/awesome-android-agent-skills

All 17
1aj-geddes avatarapp-store-deploymentaj-geddes/useful-ai-promptsDeploy iOS and Android apps to App Store and Google Play. Covers signing, versioning, build configuration, submission process, and release management.569skills.sh installs
2new-silvermoon avatarandroid-coroutinesnew-silvermoon/awesome-android-agent-skillsAuthoritative rules and patterns for production-quality Kotlin Coroutines onto Android. Covers structured concurrency, lifecycle integration, and reactive strea569skills.sh installs
3plugin87 avatardesign-codeplugin87/ux-ui-agent-skillsGenerate production-ready, accessible, token-driven component code for ANY framework — React+Tailwind, Next.js, SwiftUI, Vue, Svelte, Angular, Solid, Web Compon569skills.sh installs
4aj-geddes avatarmobile-app-debuggingaj-geddes/useful-ai-promptsDebug issues specific to mobile applications including platform-specific problems, device constraints, and connectivity issues.568skills.sh installs
5aj-geddes avatarsecurity-audit-loggingaj-geddes/useful-ai-promptsImplement comprehensive security audit logging for compliance, forensics, and SIEM integration. Use when building audit trails, compliance logging, or security 568skills.sh installs
6aj-geddes avatarbundle-size-optimizationaj-geddes/useful-ai-promptsReduce JavaScript and CSS bundle sizes through code splitting, tree shaking, and optimization techniques. Improve load times and overall application performance567skills.sh installs