Skip to content

Instantly share code, notes, and snippets.

@theCrab
Created February 20, 2026 15:38
Show Gist options
  • Select an option

  • Save theCrab/e6c1721e27ac7d3acc8e741f9cfc66c5 to your computer and use it in GitHub Desktop.

Select an option

Save theCrab/e6c1721e27ac7d3acc8e741f9cfc66c5 to your computer and use it in GitHub Desktop.
Coroutines in Kotlin Android (how to run delay() or blocking routines)
import kotlinx.coroutines.*
// . A simple PAUSE. This example simulates a background task that "waits" before continuing.
fun main() = runBlocking {
println("Task started...")
delay(2000L) // Pause for 2 seconds (2000 milliseconds)
println("Task finished after 2 seconds!")
}
// OR
// 2. Repeated Task (Polling/Clock)
// You can use delay() inside a loop to create a recurring event, like a timer or a network refresh.
@Composable
fun SplashScreen(onTimeout: () -> Unit) {
LaunchedEffect(Unit) {
delay(3000L) // Show splash for 3 seconds
onTimeout() // Navigate to Main Screen
}
// ... your Splash UI code ...
}
// OR
// 3. Simulating a Splash Screen (Android)
// Commonly used in a LaunchedEffect in Jetpack Compose to navigate after a set time.
val job = CoroutineScope(Dispatchers.Main).launch {
while (isActive) {
updateUI() // Your function to refresh the screen
delay(5000L) // Wait 5 seconds before the next update
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment