Skip to content
Back to skills

Android State

ASecurity

Configure ViewModel state emission with StateFlow, sealed UiState classes, and lifecycle-safe collection in Android. Use when working with ViewModels, UiState patterns, or exposing state to Compose UI.

  • 549 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added June 6, 2026
development

Security analysis

A100/100

Pro scans all 3 files and shows the line behind each finding

Scanned June 6, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill android-state --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Android State?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Android State
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-android-state/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-android-state)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: android-state
description: Configure ViewModel state emission with StateFlow, sealed UiState classes, and lifecycle-safe collection in Android. Use when working with ViewModels, UiState patterns, or exposing state to Compose UI.
metadata:
  triggers:
    files:
    - '**/*ViewModel.kt'
    - '**/*UiState.kt'
    keywords:
    - viewmodel
    - stateflow
    - livedata
    - uistate
    - MutableStateFlow
    - collectAsState
    - viewModelScope
    - UiState
---
# Android State Management

## **Priority: P0**

## 1. Structure ViewModel

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

See [templates](references/implementation.md) 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](references/implementation.md) 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

- [Templates](references/implementation.md)

Files in this skill

  • SKILL.md1.7 KB
  • evals/evals.json3 KB
  • references/implementation.md1.8 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.