android-coroutines

Enforce Kotlin Coroutines patterns with lifecycle-safe collection and StateFlow.

Updated Mar 9, 2025
One-click install
npx skills add https://github.com/rock-hu/vitamin-compose --skill android-coroutines-rock-hu
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-coroutines
Source: https://github.com/rock-hu/vitamin-compose/tree/main/.github/skills/android-coroutines
Command: npx skills add https://github.com/rock-hu/vitamin-compose --skill android-coroutines-rock-hu

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill provides authoritative guidelines for writing production-quality Kotlin Coroutines on Android, focusing on structured concurrency, lifecycle safety, and reactive streams to improve app reliability and responsiveness.

Core Features & Use Cases

  • Structured Concurrency: Ensure proper coroutine scope usage and task coordination.
  • Lifecycle Integration: Managing scopes like viewModelScope and lifecycleScope, and safe collection using repeatOnLifecycle.
  • Reactive Streams: Working with Flow, StateFlow, SharedFlow, and callbackFlow to model UI state and data streams.
  • Error Handling & Cancellation: Implementing CoroutineExceptionHandler, SupervisorJob, and cooperative cancellation patterns.

Quick Start

Use the android-coroutines skill to implement a sample ViewModel with viewModelScope, StateFlow state, and repeatOnLifecycle for UI state updates.

Frequently Asked Questions about android-coroutines

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

FAQPage Schema
How do I collect Kotlin Flow safely in Android to avoid lifecycle crashes?

To collect Kotlin Flow safely, use repeatOnLifecycle to ensure collection only happens during active lifecycle states. This prevents coroutine leaks and crashes when the UI is destroyed.

What is the best way to manage UI state with Kotlin Coroutines on Android?

Managing UI state with Kotlin Coroutines is best done using StateFlow within a ViewModel. Expose immutable state and update it via viewModelScope to ensure configuration survival and lifecycle safety.

How do I inject dispatchers for testing Android Coroutines?

Injecting dispatchers for Android Coroutines testing requires an injector-based dispatcher management pattern. This allows you to swap background dispatchers with TestDispatcher to control execution order in unit tests.

Why does my Android coroutine leak memory after screen navigation?

Android coroutines leak memory after navigation when scopes are mismanaged. Ensure structured concurrency by using viewModelScope or lifecycleScope, which automatically cancel ongoing tasks when their bound scope ends.

When should I use SharedFlow instead of StateFlow for Android events?

Use SharedFlow instead of StateFlow for Android events like navigation or snackbar messages. SharedFlow handles one-time event broadcasting without retaining state, whereas StateFlow is designed to hold and emit the latest UI state.