Created
February 20, 2026 15:38
-
-
Save theCrab/e6c1721e27ac7d3acc8e741f9cfc66c5 to your computer and use it in GitHub Desktop.
Coroutines in Kotlin Android (how to run delay() or blocking routines)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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