android-viewmodel

Manage Android UI state with ViewModel, StateFlow, and SharedFlow.

Updated Dec 4, 2025
One-click install
npx skills add https://github.com/thinhtt264/Snaplet-App --skill android-viewmodel
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-viewmodel
Source: https://github.com/thinhtt264/Snaplet-App/tree/main/.cursor/skills/android-viewmodel
Command: npx skills add https://github.com/thinhtt264/Snaplet-App --skill android-viewmodel

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It provides a structured pattern for building robust Android UIs by using ViewModel with StateFlow to represent persistent UI state and SharedFlow for one-off events, ensuring state survives configuration changes and events don't re-trigger on rotation.

Core Features & Use Cases

  • State-driven UI with StateFlow<UiState> backed by a private MutableStateFlow and exposed as a read-only StateFlow, ensuring thread-safe, reactive UI updates.
  • One-off events with SharedFlow<UiEvent> configured with replay = 0 to prevent repeated events after configuration changes, with suspend or tryEmit sending mechanisms.
  • UI integration guidance for Compose (collectAsStateWithLifecycle) and Views (repeatOnLifecycle) to observe state and events; architecture guidance to delegate work to UseCases or Repositories and to scope coroutines in viewModelScope.
  • Real-world use case: A login screen that shows loading, success, or error states and navigates on success.

Quick Start

Create a simple ViewModel that initializes a StateFlow for UI state, a SharedFlow for one-off events, and a sample Compose screen observing these flows.

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

Handle one-off UI events in Android by configuring a SharedFlow with a replay equal to 0. This prevents events from re-triggering after configuration changes like screen rotations, using either suspend or tryEmit sending mechanisms.

How do I observe StateFlow in Jetpack Compose with lifecycle awareness?

Observe StateFlow in Jetpack Compose by using the collectAsStateWithLifecycle function. This ensures UI state collection respects Android lifecycle events, preventing unnecessary updates when the app is in the background.

How do I handle one-off UI events in Android without duplicating them on rotation?

Prevent duplicate one-off UI events during rotation by using a SharedFlow configured with replay = 0. This ensures navigation or toast events are emitted once and do not re-trigger when the screen is recreated.

Does this Android ViewModel StateFlow pattern work with standard Views?

Yes, the pattern works with standard Android Views. You can observe the StateFlow and SharedFlow using the repeatOnLifecycle function to safely collect UI state and one-off events in a lifecycle-aware manner.

Why use StateFlow instead of LiveData for Android UI state?

Use StateFlow for Android UI state to enforce an initial value and leverage structured concurrency with viewModelScope. Unlike LiveData, it integrates seamlessly with Kotlin coroutines and requires a private MutableStateFlow exposed as read-only.