android-viewmodel

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

10.4k|519|Updated Apr 15, 2023
One-click install
npx skills add https://github.com/maxrave-dev/SimpMusic --skill android-viewmodel-maxrave-dev
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-viewmodel
Source: https://github.com/maxrave-dev/SimpMusic/tree/main/.claude/skills/android-viewmodel
Command: npx skills add https://github.com/maxrave-dev/SimpMusic --skill android-viewmodel-maxrave-dev

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides guidance on implementing Android ViewModels effectively, focusing on modern state management patterns using StateFlow and SharedFlow for robust UI updates and event handling.

Core Features & Use Cases

  • UI State Management: Learn to manage persistent UI states (loading, success, error) using StateFlow.
  • Event Handling: Understand how to handle transient UI events (toasts, navigation) with SharedFlow.
  • Lifecycle Awareness: Implement best practices for collecting flows within the Android Activity/Fragment lifecycle.
  • Use Case: When building a data-fetching screen, use this Skill's patterns to display a loading indicator, show the fetched data upon success, and present an error message if the fetch fails, all while ensuring state is preserved across configuration changes.

Quick Start

Implement an Android ViewModel using StateFlow for UI state and SharedFlow for one-off events, collecting them in a Compose UI using collectAsStateWithLifecycle and LaunchedEffect.

Frequently Asked Questions about android-viewmodel

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

FAQPage Schema
How do I manage UI state in Android Jetpack Compose using StateFlow?

Handle transient UI events in Android ViewModels by exposing a SharedFlow, then collect these one-off events in Compose using LaunchedEffect to trigger actions like navigation or showing toasts without replaying state.

How do I handle one-off UI events like navigation with SharedFlow in Android ViewModels?

Handle transient UI events in Android ViewModels by exposing a SharedFlow, then collect these one-off events in Compose using LaunchedEffect to trigger actions like navigation or showing toasts without replaying state.

What is the best way to preserve UI state across configuration changes in Android?

Android ViewModel best practices dictate using StateFlow for persistent UI state and SharedFlow for one-off events, ensuring lifecycle-aware collection to prevent memory leaks and redundant updates in Compose or traditional Views.

When should I use StateFlow versus SharedFlow for Android UI state management?

Use StateFlow for persistent UI state management because it retains the latest value and replays it to new collectors, whereas SharedFlow is suited for one-off events like navigation or toasts where state replay is unnecessary.