viewmodel

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

127|8|Updated Apr 6, 2026
One-click install
npx skills add https://github.com/hanamizuki/solopreneur --skill viewmodel-hanamizuki
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: viewmodel
Source: https://github.com/hanamizuki/solopreneur/tree/main/plugins/android-dev/skills/viewmodel
Command: npx skills add https://github.com/hanamizuki/solopreneur --skill viewmodel-hanamizuki

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Efficiently manage UI state and one-off events in Android apps using a clean ViewModel pattern with StateFlow and SharedFlow, improving testability and resilience across configuration changes.

Core Features & Use Cases

  • State management: Persist UI state with StateFlow and a private backing MutableStateFlow.
  • One-off events: Deliver transient events via SharedFlow with replay = 0 to avoid duplicate events on rotation.
  • Lifecycle integration: Collect state and events in Compose or Views using lifecycle-aware APIs and viewModelScope.

Quick Start

Create a ViewModel with a private MutableStateFlow, expose a public StateFlow, and emit UI events through a SharedFlow to drive UI updates.

Frequently Asked Questions about viewmodel

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

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

Use a private MutableStateFlow backing a public StateFlow inside a ViewModel to persist UI state robustly across configuration changes. This pattern keeps state lifecycle-aware and testable when collected in activities or fragments.

How do I deliver one-off events in Android without duplicate emissions on rotation?

Deliver one-off events by emitting them through a SharedFlow configured with replay = 0 inside your ViewModel. This prevents duplicate transient events from being re-emitted to the UI during device rotation or configuration changes.

What is the best way to collect StateFlow and SharedFlow in Android Compose?

The best way to collect StateFlow and SharedFlow in Compose is using lifecycle-aware collection APIs within viewModelScope. This ensures state and events are only collected when the Compose UI is active, preventing unnecessary updates.

Why do I need a private backing property for StateFlow in an Android ViewModel?

You need a private backing MutableStateFlow to encapsulate state mutation logic within the ViewModel. Exposing only a public StateFlow prevents external classes from modifying the UI state directly, maintaining a clean unidirectional data flow.

Does this StateFlow and SharedFlow pattern work with traditional Android Views or only Compose?

This StateFlow and SharedFlow pattern works with both Compose and traditional Views. It utilizes lifecycle-aware APIs to ensure proper state collection across different Android UI toolkits within activities and fragments.