ronjunevaldoz/kmp-agent-skills/kmp-compose-state-container
Claude Code and agent skill by ronjunevaldoz/kmp-agent-skills
- community
Checking that the SKILL.md file is still in the repository.
Choosing the right state container in Compose Multiplatform: remember vs rememberSaveable vs ViewModel vs rememberCoroutineScope. Covers: what survives recomposition, config changes, and process death; when each container applies; rememberSaveable with custom Saver for complex types; ViewModel scoping to nav back-stack entries; and the most common wrong choices (ViewModel for dropdown state, remember for form data that must survive rotation). Zero new dependencies.
Install
npx vskill@latest install ronjunevaldoz/kmp-agent-skills/kmp-compose-state-containerSKILL.md
The same SKILL.md was published earlier as ronjunevaldoz/kmp-agent-skills/kmp-compose-state-container.
When to Use This Skill
Use when you need to:
- Decide whether state belongs in
remember,rememberSaveable, or aViewModel - Understand what "survives recomposition" vs "survives config change" actually means
- Implement a custom
SaverforrememberSaveablewith a non-bundleable type - Scope a ViewModel to a nav back-stack entry vs the whole nav graph
- Diagnose state being lost on screen rotation or back navigation
Trigger keywords: remember vs ViewModel, rememberSaveable, state container, when to use ViewModel, ephemeral state, state survival, config change state, process death state, custom Saver, nav-scoped ViewModel, state lost on rotation.
Freshness rule: Compose state survival and nav-scoped ViewModel APIs change with lifecycle and navigation releases — recheck the Jetpack and CMP docs before upgrading.
Recommendation First
Default decision: ephemeral UI state → `remember`; form/input state → `rememberSaveable`; business data or screen state → `ViewModel`.
Why:
rememberis enough for transient state (dropdown open, tooltip visible) — no persistence neededrememberSaveablesurvives config changes, preventing form input loss on rotationViewModelis the correct boundary for state that outlives a single screen instance and must
survive the back-stack navigation lifecycle
Do not put business logic or domain objects inside remember or rememberSaveable — that belongs in the ViewModel, not the composition.
What Each Container Survives
| Container | Recomposition | Config change (rotation) | Process death | Scope | |
|---|---|---|---|---|---|
remember {} | ✓ | ✗ | ✗ | Composition lifetime | |
rememberSaveable {} | ✓ | ✓ | ✓ (Bundle types only) | Composition + SavedState | |
ViewModel | ✓ | ✓ | ✗ (unless SavedStateHandle) | Nav back-stack entry or nav graph | |
ViewModel + SavedStateHandle | ✓ | ✓ | ✓ | Nav back-stack entry or nav graph |
Recomposition — Compose re-executes the composable body when state changes. remember keeps the value alive across re-executions of the same composable instance.
Config change — Android destroys and recreates the Activity (screen rotation, locale change, font size change). remember is lost; rememberSaveable and ViewModel survive.
Process death — Android kills the process entirely (low memory). Only rememberSaveable (via Bundle/SavedState) and ViewModel with SavedStateHandle survive.
The Decision Tree
remember {} — In-Composition Memory
remember keeps a value alive for the lifetime of the composable in the composition. When the composable leaves the tree and re-enters (back navigation, conditional rendering), remember starts fresh.
remember with keys
When a remembered value depends on inputs, use keys. The value is recomputed when any key changes:
rememberSaveable {} — Rotation-Proof Local State
Full content: references/remembersaveable.md.
ViewModel — Config-Change-Proof Business State
Full content: references/viewmodel.md.
ViewModel Scoping in Navigation Compose
By default, koinViewModel() / viewModel() scopes the ViewModel to the current back-stack entry. When you navigate back, the ViewModel is cleared.
To share a ViewModel across multiple destinations in a nested nav graph:
This is the correct pattern for multi-step flows (checkout, onboarding) where all steps share state — the ViewModel lives as long as the user is anywhere in the graph.
rememberCoroutineScope — Composable-Scoped Coroutines
When you need to launch a coroutine from a composable (not from a ViewModel), use rememberCoroutineScope(). The scope is cancelled when the composable leaves the tree.
Use `rememberCoroutineScope` when:
- The coroutine must be tied to the composable lifecycle, not the ViewModel
- You need to launch a coroutine from a click handler (not from a
LaunchedEffect) - The coroutine triggers UI-only side effects (scroll, show keyboard, haptic)
Do NOT use `rememberCoroutineScope` for:
- API calls or repository access — those belong in
viewModelScope - State that needs to outlive the composable
Quick Reference
derivedStateOf — Memoized Derived State
When a value is derived from other State but changes less frequently than its inputs, wrap it in derivedStateOf so Compose only recomposes consumers when the derived value actually changes.
Always wrap derivedStateOf in remember — without it, a new DerivedState is created each recomposition and the memoization is lost.
See kmp-compose-state-hoisting for full decision table and examples.
snapshotFlow — Compose State as a Flow
snapshotFlow converts Compose State into a Flow so you can apply coroutine operators (debounce, distinctUntilChanged, flatMapLatest) to Compose state changes.
Use snapshotFlow when | Avoid it when | |
|---|---|---|
| You need to debounce or throttle Compose state changes | The value already comes from a StateFlow — collect it normally | |
You need distinctUntilChanged on a Compose state | The transformation is pure — derivedStateOf is lighter | |
| Bridging Compose state to analytics, accessibility, or an external SDK |
rememberUpdatedState — Latest Value Without Restarting an Effect
When a LaunchedEffect(Unit) captures a value or lambda that might change between recompositions, wrap the captured value with rememberUpdatedState. The effect keeps a stable reference that always points to the latest value — without restarting the coroutine.
When to use:
LaunchedEffect(Unit)that reads a callback or config value that may change- Any long-running effect that should NOT restart when a captured value changes
When NOT needed:
- Effect key already tracks the value (the effect will restart cleanly on key change)
- The captured value is
valand provably never reassigned
Common Mistakes
1. `remember` for form data A registration form with remember loses all user input on screen rotation. Use rememberSaveable for form fields, or a ViewModel if the form has validation logic.
2. ViewModel for every bit of UI state isDropdownOpen, isTooltipVisible, expandedCardIndex — none of these belong in a ViewModel. They're ephemeral, local, and have no business logic. remember is correct.
3. `rememberSaveable` for large collections Bundle has a size limit. Saving a list of 500 products in rememberSaveable will crash with a TransactionTooLargeException on Android. Save only IDs; reload data from cache/repo.
4. Creating a new ViewModel scope per recomposition viewModel() / koinViewModel() must not be called inside a loop or conditional — it creates a new ViewModel per call. Call it once at the screen level.
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/kmp-compose-state-container/SKILL.md
Popularity
- 7-day trend score
- 5
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/ronjunevaldoz/kmp-agent-skills/kmp-compose-state-container)