android-viewmodel

Generate Kotlin ViewModels with MutableSharedFlow event handling for Android apps.

Updated Nov 3, 2025
One-click install
npx skills add https://github.com/devanfer02/telnetquiz --skill android-viewmodel-devanfer02
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-viewmodel
Source: https://github.com/devanfer02/telnetquiz/tree/main/.claude/skills/android-viewmodel
Command: npx skills add https://github.com/devanfer02/telnetquiz --skill android-viewmodel-devanfer02

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps Android developers implement robust UI state management using StateFlow and reliable one-off events with SharedFlow, reducing boilerplate and lifecycle-related bugs.

Core Features & Use Cases

  • State management with StateFlow-backed UI state (private MutableStateFlow and public read-only StateFlow).
  • One-off events with SharedFlow (replay = 0) to handle navigation, toasts, and snackbars.
  • Lifecycle-safe collection patterns in Compose and Views.

Quick Start

Create an android ViewModel example that uses StateFlow for UI state and SharedFlow for one-off events with proper initialization, exposure, and safe updates.

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 handle one-off events?

You can manage Android UI state by initializing a private MutableStateFlow with an initial value and exposing it as a public read-only StateFlow. For one-off events like navigation or toasts, use a MutableSharedFlow with replay set to 0.

What is the best way to handle transient events in Android ViewModel without repeating them?

The best way to handle transient events in an Android ViewModel is using SharedFlow with replay = 0. This ensures one-off events like snackbars or navigation commands are emitted thread-safely and delivered only once, preventing repeated emissions on configuration changes.

How do I ensure Android ViewModel state survives configuration changes using StateFlow?

To ensure UI state survives configuration changes, store the state within an Android ViewModel using a MutableStateFlow. The ViewModel lifecycle outlives the UI recreation, and the StateFlow retains the current UI state value for the newly created Compose or View component to collect.

How do I safely update StateFlow and emit SharedFlow events in an Android ViewModel?

You can safely update StateFlow and emit SharedFlow events in an Android ViewModel by applying thread-safe update mechanisms. Use the update function for StateFlow to guarantee atomic state modifications and SharedFlow's emit function for thread-safe event delivery.

Does this Android ViewModel StateFlow pattern work with both Compose and Views?

Yes, this Android ViewModel StateFlow pattern works with both Compose and Views. The Skill provides lifecycle-safe collection patterns for both UI paradigms, allowing you to collect the public StateFlow and SharedFlow without risking crashes or duplicate event handling.

Why use StateFlow instead of LiveData for Android ViewModel UI state?

You should use StateFlow for Android ViewModel UI state to gain explicit threading control and distinct lifecycle-safe collection patterns. StateFlow requires an initial value, ensuring the UI always has a default state to render, whereas SharedFlow handles one-off events without the sticky behavior of LiveData.