android-viewmodel

Manage Android UI state with ViewModel, StateFlow, and SharedFlow.

1|2|Updated Jan 25, 2026
One-click install
npx skills add https://github.com/jhparktime/OnGuard --skill android-viewmodel-jhparktime
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: android-viewmodel
Source: https://github.com/jhparktime/OnGuard/tree/main/.claude/skills/android-viewmodel
Command: npx skills add https://github.com/jhparktime/OnGuard --skill android-viewmodel-jhparktime

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Android apps often struggle to maintain robust, lifecycle-safe UI state and single-use events across config changes. This Skill shows how to use ViewModel with StateFlow for persistent UI state and SharedFlow for single-use events like navigation or toasts.

Core Features & Use Cases

  • State management with StateFlow for persistent UI state
  • One-off event handling with SharedFlow for navigation and messages
  • Lifecycle-safe, testable patterns using viewModelScope and coroutines

Quick Start

Create a ViewModel with a private MutableStateFlow, expose a read-only StateFlow, and emit SharedFlow events for transient actions.

Frequently Asked Questions about android-viewmodel

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I manage persistent UI state in Android across configuration changes?▼

To manage persistent UI state across configuration changes, use a ViewModel with StateFlow. Store your state in a private backing MutableStateFlow and expose a public read-only StateFlow to ensure the state survives lifecycle events safely.

What is the best way to handle one-off events like navigation in Android ViewModels?▼

The best way to handle one-off events like navigation or toasts is using SharedFlow with replay = 0. This ensures transient events are emitted only once and are not duplicated when configuration changes recreate the UI.

Why does my Android StateFlow emit stale values after rotation?▼

Stale values after rotation often occur when state is not properly lifecycle-safe. Using a ViewModel with StateFlow as a UI state container with an initial value ensures the state persists correctly during configuration changes.

Can I use SharedFlow for both persistent state and one-off events?▼

No, you should use separate flows. Use StateFlow for persistent UI state with an initial value and SharedFlow with replay = 0 for one-off events. This separation keeps your Android ViewModel state and event management clean.

How do I expose a read-only StateFlow from an Android ViewModel?▼

To expose a read-only StateFlow, create a private MutableStateFlow as a backing property and expose it as a public StateFlow. This pattern prevents external modifications while allowing the Android ViewModel to update the UI state internally.

Do I need viewModelScope and coroutines to test Android StateFlow UI state?▼

Yes, using viewModelScope and coroutines provides lifecycle-safe and testable patterns for StateFlow UI state. This approach ensures your Android ViewModel manages state and one-off events correctly during asynchronous operations.