collecting-flows-safely

Migrates Compose UI flow collection to lifecycle-aware collectAsStateWithLifecycle with conflation and hoisting patterns.

Updated May 31, 2026
One-click install
npx skills add https://github.com/decoutkhanqindev/Lich-Viet-Loc-Phat --skill collecting-flows-safely-decoutkhanqindev
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: collecting-flows-safely
Source: https://github.com/decoutkhanqindev/Lich-Viet-Loc-Phat/tree/main/.claude/skills/collecting-flows-safely
Command: npx skills add https://github.com/decoutkhanqindev/Lich-Viet-Loc-Phat --skill collecting-flows-safely-decoutkhanqindev

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Compose screens that use collectAsState() keep collecting flows while the app is backgrounded, wasting CPU and battery, and composables that accept Flow parameters block skipping and recompose on every emission. This Skill migrates those patterns to lifecycle-aware collection so upstream work pauses when the UI is not visible. ## Core Features & Use Cases - Lifecycle-aware migration: Replaces every collectAsState() call with collectAsStateWithLifecycle() from androidx.lifecycle:lifecycle-runtime-compose, with guidance on initialValue and minActiveState (STARTED vs RESUMED). - Flow parameter hoisting: Removes the Flow-as-composable-parameter antipattern by collecting at the caller and passing resolved values or lambda providers. - High-frequency stream tuning: Applies .conflate() and .distinctUntilChanged() inside remember() for sensor, location, and websocket streams, plus snapshotFlow for State-to-Flow bridging. - Use Case: A chat screen polls a repository StateFlow and drains battery in the recents tray; apply this Skill to switch to collectAsStateWithLifecycle, conflate the stream, and verify with logcat that upstream work stops when backgrounded. ## Quick Start Ask the assistant to audit this Compose module for collectAsState usages and migrate them to lifecycle-aware collection with proper conflation.

Frequently Asked Questions about collecting-flows-safely

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I replace collectAsState with collectAsStateWithLifecycle in Compose?▼

Add the androidx.lifecycle:lifecycle-runtime-compose dependency and swap each collectAsState() call for collectAsStateWithLifecycle(). For StateFlow no initial value is needed; cold flows and SharedFlow require an initialValue parameter.

collectAsState vs collectAsStateWithLifecycle: what is the difference?▼

collectAsState keeps collecting whenever the composable is in composition, including while backgrounded. collectAsStateWithLifecycle ties collection to a Lifecycle.State (default STARTED), so upstream work pauses when the app is not visible, saving CPU and battery.

Why is passing a Flow as a composable parameter bad?▼

Flow is an unstable type, so it blocks skipping for the entire composable and its collection lifecycle is unclear across recompositions. Collect at the caller with collectAsStateWithLifecycle and pass the resolved value, or a lambda provider for very hot producers.

How do I handle high-frequency sensor flows in Jetpack Compose?▼

Apply .conflate() and .distinctUntilChanged() upstream of collectAsStateWithLifecycle, wrapped in remember(key) so the operator chain is not recreated per recomposition. Ensure the emitted type has a correct equals() for distinctUntilChanged to work.

When should I use snapshotFlow instead of derivedStateOf?▼

Use snapshotFlow inside a LaunchedEffect when the only consumer is a fire-and-forget effect like analytics or logging, since reads happen in the coroutine rather than the composition phase. Prefer derivedStateOf when the result feeds back into UI.

When should I not use collectAsStateWithLifecycle?▼

Avoid it for flows created and consumed inside a single composable's remember block, for data already held as Compose State, and for truly always-on background listeners. Background work that must run while stopped belongs in a Service or WorkManager.