Android State Management

SkillSearch

Configure ViewModel state emission with StateFlow, SharedFlow, and sealed UiState classes in Android. Use for state modeling and sharing policies; defer generic lifecycle collection, navigation-event plumbing, Hilt injection, test code, and configuration-change recipes to their specific skills.

Available today. Use it from your connected AI after setup.

Connect ahel once, and every AI you use reads what you have installed.

Then ask your AI: use the Android State Management skill

What this skill tells your AI

The instructions your AI receives, as published by hoangnguyen0403/agent-skills-standard in skills/android/android-state/SKILL.md and read by ahel’s review.

Priority: P0 (CRITICAL)

1. Structure ViewModel

  • Expose ONE StateFlow<UiState> via .asStateFlow().
  • Use viewModelScope for all coroutines.
  • Trigger initial load in init block.

See templates for ViewModel and UiState examples.

2. Define UI State (LCE Pattern)

  • Use sealed interface with Loading, Content, Error variants.
  • Mark data classes @Immutable.

See templates for sealed UiState pattern.

3. Collect State Lifecycle-Safely

  • Use collectAsStateWithLifecycle() in Compose.
  • Use SharingStarted.WhileSubscribed(5000) for shared resources.

Anti-Patterns

  • No LiveData for New Code: Use StateFlow — lifecycle-safe and Compose-compatible.
  • No Public MutableStateFlow: Expose only .asStateFlow() to consumers.
  • No Context in ViewModel: Leaks Activity. Use Application context if truly needed.

Verification

  • Each ViewModel exposes exactly one StateFlow<UiState>.
  • UiState is a sealed interface with Loading, Content, Error variants.
  • UI collects with collectAsStateWithLifecycle().
  • ./gradlew test passes for ViewModel tests.

References

Signals

GitHub stars
565
Forks
163
Last commit
Sep 2026
Advanced
Catalog kind
skill
Gateway key
android-state
Source
github.com/hoangnguyen0403/agent-skills-standard