ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

Kotlin Tutorial• 7 min read

StateFlow & Jetpack Compose Modern Android UI

Declarative UI, StateFlow reactivity, and unidirectional data flow

Modern Android development uses Jetpack Compose (a declarative UI toolkit similar to React/Flutter) driven by reactive Kotlin StateFlow streams.

Declarative UI with @Composable

@Composable: Functions that define UI components declaratively from state.

Unidirectional Data Flow (UDF): State flows down from ViewModels; events flow up from UI interactions.

StateFlow: A state-holder observable flow that emits the current and new state updates to active UI collectors.

Jetpack Compose Composable Function Concept
@Composable
fun CourseCard(title: String, lessonsCount: Int, onEnroll: () -> Unit) {
    Card(modifier = Modifier.padding(16.dp).fillMaxWidth()) {
        Column(modifier = Modifier.padding(16.dp)) {
            Text(text = title, style = MaterialTheme.typography.titleLarge)
            Text(text = "$lessonsCount Lessons Included", color = Color.Gray)
            Spacer(modifier = Modifier.height(8.dp))
            Button(onClick = onEnroll) {
                Text("Enroll Now")
            }
        }
    }
}
Jetpack Compose eliminates XML layouts in favor of pure Kotlin code.
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which annotation in modern Android development designates a function that defines declarative UI components?