android-coroutines

Enforce structured concurrency and lifecycle-safe coroutine patterns in Android Kotlin code.

Updated Nov 3, 2025
One-click install
npx skills add https://github.com/devanfer02/telnetquiz --skill android-coroutines-devanfer02
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-coroutines
Source: https://github.com/devanfer02/telnetquiz/tree/main/.claude/skills/android-coroutines
Command: npx skills add https://github.com/devanfer02/telnetquiz --skill android-coroutines-devanfer02

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides authoritative patterns for building robust, lifecycle-aware Kotlin coroutine code on Android, reducing crashes and boilerplate through structured concurrency and Flow usage.

Core Features & Use Cases

  • Structured concurrency: correct launching, scope management, and cancellation across Android components.
  • Lifecycle integration: leveraging viewModelScope, lifecycleScope, and repeatOnLifecycle for safe collection.
  • Reactive streams: practical use of Flow, StateFlow, SharedFlow, and callbackFlow.
  • Error handling & cancellation: employing CoroutineExceptionHandler, SupervisorJob, and cooperative cancellation patterns.
  • Testing readiness: setting up TestDispatcher and runTest for reliable tests.
  • Use cases: fetch API data, perform background work, and reflect UI state updates with minimal boilerplate.

Quick Start

Add coroutines support to your Android project, adopt viewModelScope for coroutines in ViewModels, inject a dispatcher through constructors, and collect flows within repeatOnLifecycle blocks. Use standard testing patterns (StandardTestDispatcher and runTest) to verify behavior.

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 flows safely during the Android lifecycle?

To safely collect Kotlin flows during the Android lifecycle, use repeatOnLifecycle within lifecycleScope to ensure collection only occurs when the lifecycle is active, preventing crashes and resource waste.

What is the best way to inject dispatchers for main-safe suspend functions in Android?

To achieve main-safe suspend functions, use dependency injection to pass dispatchers via constructors. This allows easy context switching for background work and facilitates reliable testing using TestDispatcher.

How do I test Kotlin coroutines in Android using runTest?

To test Kotlin coroutines in Android using runTest, replace real dispatchers with a StandardTestDispatcher. This eliminates flakiness by controlling virtual time and ensuring structured concurrency in ViewModel and repository tests.

When should I use SupervisorJob and CoroutineExceptionHandler for error handling?

Use SupervisorJob and CoroutineExceptionHandler when you need to prevent a child coroutine failure from cancelling siblings and to centrally manage uncaught exceptions. This maintains structured concurrency without prematurely terminating the scope.

How do I convert callbacks to Kotlin flows using callbackFlow?

To convert callbacks to Kotlin flows using callbackFlow, bridge existing callback-based APIs into reactive streams. This suspends execution and manages resource cleanup automatically upon cancellation, integrating legacy code with structured concurrency.

Why does my Android ViewModel coroutine leak memory across configuration changes?

Your Android ViewModel coroutine leaks memory across configuration changes if it is not launched within viewModelScope. Using viewModelScope ensures structured concurrency and automatic cancellation when the ViewModel is cleared.