android-viewmodel

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

Updated May 29, 2016
One-click install
npx skills add https://github.com/AerisG222/maw-photos-android --skill android-viewmodel-aerisg222
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-viewmodel
Source: https://github.com/AerisG222/maw-photos-android/tree/main/.github/skills/android-viewmodel
Command: npx skills add https://github.com/AerisG222/maw-photos-android --skill android-viewmodel-aerisg222

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides clear guidelines and best practices for implementing Android ViewModels, focusing on efficient state management for modern Android development using Jetpack Compose and Views.

Core Features & Use Cases

  • StateFlow for UI State: Manages persistent UI states (loading, success, error) with proper initialization and thread-safe updates.
  • SharedFlow for Events: Handles transient UI events (toasts, navigation) without re-triggering on configuration changes.
  • UI Collection Strategies: Demonstrates how to collect state and events in both Compose and traditional View-based UIs using lifecycle-aware methods.
  • Use Case: Ensure your Android app's UI state is robustly managed, reacting correctly to user interactions and system events while surviving configuration changes.

Quick Start

Implement the provided ViewModel pattern using StateFlow for UI state and SharedFlow 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 use StateFlow for UI state in an Android ViewModel?

Use StateFlow in an Android ViewModel to manage persistent UI states like loading, success, and error with proper initialization and thread-safe updates. This ensures robust UI state exposure that survives configuration changes.

Why does my Android ViewModel re-trigger navigation events on configuration change?

Navigation events re-trigger because they are likely exposed as StateFlow. Expose transient UI events like navigation or toasts as SharedFlow instead, which handles one-off events correctly without re-emitting them during configuration changes.

How do I collect ViewModel StateFlow in Jetpack Compose?

Collect ViewModel StateFlow in Jetpack Compose using lifecycle-aware collection methods. This ensures the UI layer only consumes state when actively visible, optimizing performance and preventing unnecessary updates.

Should I use SharedFlow or StateFlow for one-off events in Android?

Use SharedFlow for one-off events in Android. SharedFlow is designed for transient UI events like toasts and navigation, whereas StateFlow is meant for persistent UI state, ensuring events are not redundantly re-triggered.

What is the best way to manage Android ViewModel lifecycle with coroutines?

Adhere to ViewModel lifecycle management and coroutine scoping best practices to manage Android ViewModel lifecycle with coroutines, clearing coroutines automatically when the ViewModel is cleared.

Does this Android ViewModel pattern work with traditional View-based UIs?

Yes, the Android ViewModel pattern works with traditional View-based UIs. It demonstrates how to collect state and events across different Android UI toolkits using lifecycle-aware collection methods.