android-viewmodel

Implement Android ViewModels with StateFlow UI state and SharedFlow events.

1|Updated Sep 7, 2025
One-click install
npx skills add https://github.com/sahuadarsh0/Wallet --skill android-viewmodel-sahuadarsh0
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-viewmodel
Source: https://github.com/sahuadarsh0/Wallet/tree/main/.cursor/skills/android-viewmodel
Command: npx skills add https://github.com/sahuadarsh0/Wallet --skill android-viewmodel-sahuadarsh0

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Managing UI state and one-off events in Android apps is error-prone and hard to scale without clean architecture patterns.

Core Features & Use Cases

  • Use a ViewModel to hold UI state via StateFlow to survive configuration changes.
  • Emit one-off events (e.g., navigation, toasts) via a SharedFlow to avoid replay on rotation.
  • Follow best practices: private MutableStateFlow with a public read-only StateFlow, and update state safely with .update.

Quick Start

Create a ViewModel that uses a private MutableStateFlow and a public StateFlow for UI state and a MutableSharedFlow for one-off events.

Frequently Asked Questions about android-viewmodel

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

FAQPage Schema
How do I manage Android UI state with StateFlow and ViewModel?

To handle one-off events in Android, emit them via a SharedFlow configured with replay = 0 to prevent event replay on rotation. This distinguishes persistent UI state from transient actions like navigation or toasts, ensuring predictable event handling.

How do I handle one-off navigation events in Android without replaying them on rotation?

To handle one-off events in Android, emit them via a SharedFlow configured with replay = 0 to prevent event replay on rotation. This distinguishes persistent UI state from transient actions like navigation or toasts, ensuring predictable event handling.

Why does my Android ViewModel lose UI state on configuration changes?

The best practice for exposing Android ViewModel state is to keep a private MutableStateFlow for internal modifications and expose a public read-only StateFlow. This encapsulation ensures safe state updates via .update while preventing external uncontrolled mutations.

What's the best practice for exposing read-only StateFlow from an Android ViewModel?

The best practice for exposing Android ViewModel state is to keep a private MutableStateFlow for internal modifications and expose a public read-only StateFlow. This encapsulation ensures safe state updates via .update while preventing external uncontrolled mutations.

When should I use SharedFlow instead of StateFlow for Android events?

Use SharedFlow instead of StateFlow for Android one-off events like navigation or toasts. Configuring SharedFlow with replay = 0 prevents redundant event emissions on configuration changes, whereas StateFlow is designed for holding persistent UI state.