android-viewmodel

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

Updated Mar 9, 2025
One-click install
npx skills add https://github.com/rock-hu/vitamin-compose --skill android-viewmodel-rock-hu
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-viewmodel
Source: https://github.com/rock-hu/vitamin-compose/tree/main/.github/skills/android-viewmodel
Command: npx skills add https://github.com/rock-hu/vitamin-compose --skill android-viewmodel-rock-hu

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Provides best practices for implementing Android ViewModels that hold UI state with StateFlow and manage transient events with SharedFlow, ensuring UI resilience across configuration changes and clean separation of concerns.

Core Features & Use Cases

  • State management: private MutableStateFlow with a public read-only StateFlow, including an initial value and thread-safe updates.
  • Event handling: use SharedFlow for one-off events like navigation and toasts, with replay = 0 to prevent re-triggering on rotation.
  • Lifecycle integration: use viewModelScope for coroutines; integrate with Compose via collectAsStateWithLifecycle; for traditional Views, use repeatOnLifecycle to collect state.

Quick Start

Create a ViewModel that exposes a private MutableStateFlow and a public StateFlow, and a private MutableSharedFlow for events. Collect state in Compose with collectAsStateWithLifecycle() and trigger events with emit or tryEmit.

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

To manage Android UI state with StateFlow and ViewModel, expose a private MutableStateFlow as a public read-only StateFlow, ensuring thread-safe updates and an initial value. This approach preserves UI state across configuration changes cleanly.

How do I handle navigation or toast events in Android without re-triggering them on rotation?

To handle one-off events like navigation without re-triggering on rotation, use a SharedFlow with replay = 0 in your Android ViewModel. This ensures transient events are collected only once and do not fire again when the UI recreates.

What is the best way to collect StateFlow in Jetpack Compose?

The best way to collect StateFlow in Jetpack Compose is using collectAsStateWithLifecycle. This method integrates with Android lifecycle awareness, ensuring UI state collection pauses and resumes correctly based on the Compose lifecycle.

How do I run coroutines safely within an Android ViewModel?

To run coroutines safely within an Android ViewModel, use viewModelScope. This scoped coroutine builder automatically cancels ongoing tasks when the ViewModel is cleared, preventing memory leaks and ensuring efficient resource cleanup.

Does this Android ViewModel state management approach work with traditional Views?

Yes, this Android ViewModel state management approach works with traditional Views. For traditional View systems, you should use repeatOnLifecycle to collect StateFlow and SharedFlow safely, ensuring state updates respect the Android lifecycle.

Why use MutableStateFlow instead of LiveData for holding UI state in Android?

Using MutableStateFlow instead of LiveData for UI state in Android provides explicit thread-safe updates and seamless integration with coroutines. It exposes a read-only StateFlow public API, offering a more structured way to handle complex UI state flows.