android-coroutines

Implement Android Kotlin Coroutines with structured concurrency and lifecycle-safe Flow collection.

915|88|Updated Feb 6, 2024
One-click install
npx skills add https://github.com/new-silvermoon/awesome-android-agent-skills --skill android-coroutines-new-silvermoon
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-coroutines
Source: https://github.com/new-silvermoon/awesome-android-agent-skills/tree/main/.github/skills/android-coroutines
Command: npx skills add https://github.com/new-silvermoon/awesome-android-agent-skills --skill android-coroutines-new-silvermoon

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

The Android Coroutine skill provides authoritative rules and patterns to ensure production-quality Kotlin Coroutines code on Android, enforcing structured concurrency, lifecycle safety, and modern best practices.

Core Features & Use Cases

  • Async and concurrent programming with suspend functions, dispatchers, and parallel execution.
  • Reactive streams support with Flow, StateFlow, SharedFlow, and callbackFlow.
  • Lifecycle integration using viewModelScope, lifecycleScope, and repeatOnLifecycle for safe collection.
  • Robust error handling, cancellation semantics, and testing patterns to ensure reliability.
  • Guidance for testing with TestDispatcher and runTest in unit tests.

Quick Start

Use the android-coroutines skill to implement a main-safe ViewModel that fetches data from a repository using a properly injected dispatcher and exposed StateFlow for UI state.

Frequently Asked Questions about android-coroutines

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

FAQPage Schema
How do I use Kotlin Coroutines in an Android ViewModel for safe data loading?

Use Kotlin Coroutines in an Android ViewModel by launching jobs within viewModelScope and injecting dispatchers via the constructor to ensure main safety and structured concurrency. Expose repository fetched data as StateFlow to reliably manage UI state without leaking contexts.

What is the best way to collect Flow in Android without crashing on lifecycle changes?

The best way to collect Flow in Android without lifecycle crashes is using repeatOnLifecycle alongside lifecycleScope. This pattern ensures Flow collection only happens when the lifecycle is at least started, automatically suspending and resuming collection to prevent background emission leaks.

How do I test suspend functions and Flow in Android unit tests?

Test suspend functions and Flow in Android unit tests by injecting TestDispatcher instances into your ViewModels and wrapping test bodies with runTest. This approach controls virtual time and dispatcher execution, allowing you to verify Flow emissions, cancellation semantics, and error handling deterministically.

When should I use StateFlow vs SharedFlow for Android UI state?

Use StateFlow for Android UI state when you need a single updated value holder that always has a latest state, and use SharedFlow for event-based scenarios like navigation or snackbar messages. Both integrate with viewModelScope to ensure structured concurrency and proper lifecycle handling.

Why does my Kotlin Coroutine leak memory when the Android screen is closed?

Kotlin Coroutines leak memory when the Android screen closes if launched outside viewModelScope or lifecycleScope without structured concurrency. Enforce parent-child job hierarchies and use injected dispatchers to ensure active coroutines are properly cancelled upon lifecycle destruction.