import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.*
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
Surface(modifier = Modifier.padding(16.dp)) {
AsyncDataLoaderScreen()
}
}
}
}
}
@Composable
fun AsyncDataLoaderScreen() {
var status by remember { mutableStateOf("Idle") }
var data by remember { mutableStateOf("") }
val scope = rememberCoroutineScope()
Column {
Button(onClick = {
scope.launch {
status = "Loading..."
val deferredData = async {
delay(2000) // Simulate network or heavy work
"Here is your loaded data!"
}
data = deferredData.await()
status = "Data loaded"
}
}) {
Text("Load Data")
}
Text("Status: $status", modifier = Modifier.padding(top = 16.dp))
Text("Data: $data", modifier = Modifier.padding(top = 8.dp))
}
}We use rememberCoroutineScope() to get a coroutine scope tied to the composable lifecycle. When the user taps the 'Load Data' button, we launch a coroutine in this scope.
Inside the coroutine, we first update the status to 'Loading...'. Then we use async builder to simulate fetching data with a 2-second delay. This runs concurrently and returns a deferred result.
We await() the result from async, then update the data state and set status to 'Data loaded'. This updates the UI automatically.
This approach keeps the UI responsive and shows loading feedback while data is fetched asynchronously.