android-kotlin-coroutines

Manage Android asynchronous concurrency with Kotlin coroutines, Flow, and StateFlow.

187|20|Updated Nov 20, 2025
One-click install
npx skills add https://github.com/TheBushidoCollective/han --skill android-kotlin-coroutines
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: android-kotlin-coroutines
Source: https://github.com/TheBushidoCollective/han/tree/main/jutsu/jutsu-android/skills/kotlin-coroutines
Command: npx skills add https://github.com/TheBushidoCollective/han --skill android-kotlin-coroutines

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill covers asynchronous operations in Android using Kotlin coroutines, Flow, and StateFlow for responsive UIs.

Core Features & Use Cases

  • viewModelScope: Safe coroutine scope tied to lifecycle.
  • Flow & StateFlow: Reactive UI state and data streams.
  • Dispatchers: Correct thread usage for IO, Main, and CPU.

Quick Start

Launch a coroutine in a ViewModel to fetch data and expose it via a StateFlow.

Frequently Asked Questions about android-kotlin-coroutines

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

FAQPage Schema
How do I handle asynchronous operations in Android with Kotlin coroutines?

Kotlin coroutines provide structured concurrency for Android by launching tasks in viewModelScope, which automatically cancels work when the ViewModel is destroyed. Use Dispatchers to route IO, network, and database operations off the main thread, then expose results via StateFlow for reactive UI updates.

What's the difference between Flow and StateFlow in Kotlin?

Flow is a cold, one-to-many stream that emits values only when collected and starts fresh for each collector. StateFlow is a hot, state-holding variant that always has a current value, replays it to new collectors, and is ideal for UI state that must persist across configuration changes.

How do I fetch data from a network or database and update the UI reactively?

Launch a coroutine in viewModelScope to fetch data on the IO Dispatcher, then emit results to a StateFlow. Collect the StateFlow in your UI layer to observe state changes and update views automatically without manual subscription management.

Do I need to manually cancel coroutines in Android Activities and Fragments?

No. viewModelScope automatically cancels all launched coroutines when the ViewModel is cleared, eliminating memory leaks and dangling operations. Avoid GlobalScope and lifecycle scopes; always use viewModelScope in ViewModels.

Can I handle errors and retries within coroutine flows?

Yes. Coroutines support try-catch blocks within suspend functions, and Flow operators like catch and retry handle errors and backpressure. Emit error states to StateFlow so the UI can display failures and allow user-initiated retries.

What Dispatchers should I use for different Android tasks?

Use Dispatchers.IO for network and database calls, Dispatchers.Main for UI updates, and Dispatchers.Default for heavy CPU work. Coroutines switch Dispatchers transparently; withContext(Dispatcher) changes context within a coroutine without blocking.