android-viewmodel

Implement Android ViewModel UI state with StateFlow and SharedFlow.

915|88|Updated Feb 6, 2024
One-click install
npx skills add https://github.com/new-silvermoon/awesome-android-agent-skills --skill android-viewmodel-new-silvermoon
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-viewmodel
Source: https://github.com/new-silvermoon/awesome-android-agent-skills/tree/main/.github/skills/android-viewmodel
Command: npx skills add https://github.com/new-silvermoon/awesome-android-agent-skills --skill android-viewmodel-new-silvermoon

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

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

Core Features & Use Cases

  • Expose UI state via a read-only StateFlow backed by a private MutableStateFlow.
  • Use SharedFlow for transient one-off events like navigation or toasts with replay = 0.
  • Ensure lifecycle-safe collection in Compose and traditional Views.
  • Promote testable, predictable state management with clear separation of concerns.

Quick Start

Create a ViewModel that exposes a private MutableStateFlow<T> and a public StateFlow<T>, initializes with a default value, and emits one-off events via a MutableSharedFlow<Event>(replay = 0). Collect both in the UI using collectAsStateWithLifecycle (for StateFlow) and a LaunchedEffect or repeatOnLifecycle (for SharedFlow).

Frequently Asked Questions about android-viewmodel

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

FAQPage Schema
How do I expose UI state from an Android ViewModel using StateFlow?

Expose UI state from an Android ViewModel using StateFlow by backing a public StateFlow<T> with a private MutableStateFlow<T>, initializing it with a default value to ensure predictable state management and separation of concerns.

What's the best way to handle one-off events like navigation in Android MVVM?

The best way to handle one-off events in Android MVVM is using SharedFlow with replay = 0, ensuring transient events like navigation or toasts are emitted only once without re-triggering when the UI recomposes or rotates.

How do I collect StateFlow safely with lifecycle awareness in Compose?

Collect StateFlow safely with lifecycle awareness in Compose using the collectAsStateWithLifecycle extension, which pauses collection when the app enters the background and prevents unnecessary emissions.

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

Use SharedFlow instead of StateFlow for ViewModel events when dealing with transient, one-off occurrences like navigation or toast messages, as SharedFlow with replay = 0 prevents redundant emission unlike StateFlow which retains the latest value.

How do I trigger ViewModel coroutines for state updates?

Trigger ViewModel coroutines for state updates by launching them within viewModelScope, allowing asynchronous data fetching and processing to safely update the private MutableStateFlow without leaking lifecycle-bound operations.