android-viewmodel

Implement Android ViewModels with StateFlow and SharedFlow for UI state and events.

Updated Mar 10, 2026
One-click install
npx skills add https://github.com/siddhantparadox/codexAndroidApp --skill android-viewmodel-siddhantparadox
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-viewmodel
Source: https://github.com/siddhantparadox/codexAndroidApp/tree/main/.agents/skills/android-viewmodel
Command: npx skills add https://github.com/siddhantparadox/codexAndroidApp --skill android-viewmodel-siddhantparadox

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps developers implement Android ViewModels correctly, focusing on efficient UI state management and handling one-off events to prevent common bugs and improve app responsiveness.

Core Features & Use Cases

  • UI State Management: Uses StateFlow for persistent UI states (loading, success, error) that survive configuration changes.
  • Event Handling: Employs SharedFlow with replay = 0 for transient UI events like navigation or snackbars, ensuring they are not replayed on rotation.
  • Lifecycle Awareness: Integrates with Jetpack Compose and traditional Views using lifecycle-aware collection methods.
  • Use Case: Ensure your Android app's UI state is always up-to-date and that crucial events like a "data saved" confirmation are shown exactly once, even if the user rotates their device.

Quick Start

Implement the provided ViewModel pattern in your Android project to manage UI state with StateFlow and events with SharedFlow.

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 in a ViewModel?

Manage Android UI state with StateFlow by initializing it to hold persistent loading, success, or error states that survive configuration changes, then exposing and collecting it using lifecycle-aware methods in Compose or traditional Views.

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

Handle one-off Android ViewModel events like navigation or snackbars by employing SharedFlow with replay equals zero, ensuring transient UI events are shown exactly once and not replayed on device rotation.

Why does my SharedFlow event replay on rotation in Jetpack Compose?

SharedFlow events replay on rotation if you do not configure replay equals zero; using SharedFlow with replay equals zero for transient UI events ensures they are not replayed during configuration changes.

Can I use this ViewModel pattern with both Jetpack Compose and traditional Views?

Yes, this ViewModel pattern works with both Jetpack Compose and traditional Views by integrating lifecycle-aware collection methods to ensure UI state and events are collected safely across different Android UI toolkits.

How do I update StateFlow UI state from viewModelScope in Android?

Update StateFlow UI state from viewModelScope by delegating data fetching operations to UseCases or Repositories, then emitting the resulting loading, success, or error states back into the exposed StateFlow.