Build Jetpack Compose UIs with state and navigation
✓Works with OpenClaudeYou are an Android developer building declarative UIs with Jetpack Compose, managing state lifecycle, and implementing navigation between composables.
What to check first
- Verify
build.gradlehasandroidx.compose.ui:ui,androidx.compose.material3:material3, andandroidx.navigation:navigation-composedependencies - Run
android:listin Android Studio to confirm your target API level supports Compose (API 21+) - Check that your Activity uses
setContent { }instead ofsetContentView(R.layout.xml)
Steps
- Define a
ViewModelwithmutableStateOf()to hold UI state that survives configuration changes - Create a composable function marked with
@Composablethat accepts state and callback parameters - Use
remember { }for temporary state tied to composition andrememberSaveable { }for state surviving process death - Implement a
NavControllerusingrememberNavController()and wrap navigation destinations withNavHost - Define routes as string constants or sealed classes (e.g.,
"detail/{id}"orsealed class Route) - Use
navController.navigate("route")to push destinations; usenavController.popBackStack()to go back - Extract route arguments with
backStackEntry.arguments?.getString("id")and pass to destination composables - Leverage
derivedStateOf { }to compute values from state without recomposition overhead
Code
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
// ViewModel with state
class CounterViewModel : ViewModel() {
private val _count = mutableStateOf(0)
val count: State<Int> = _count
fun increment() {
_count.value++
}
}
// Main composable with navigation
@Composable
fun AppNavigation() {
val navController = rememberNavController()
val viewModel = viewModel<CounterViewModel>()
NavHost(navController, startDestination = "home") {
composable("home") {
HomeScreen(
count = viewModel.count.value,
onIncrement = { viewModel.increment() },
onNavigateToDetail = { id ->
navController.navigate("detail/$id")
}
)
}
composable(
"detail/{id}",
arguments = listOf(navArgument("id") { type = NavType.StringType })
) { backStackEntry ->
val id = backStackEntry.arguments?.getString("id")
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Kotlin / Android Skills
Other Claude Code skills in the same category — free to download.
Room Database
Set up Room database with DAOs, entities, and migrations
Retrofit
Configure Retrofit for API calls with coroutines
Hilt DI
Set up Hilt dependency injection in Android
Kotlin Coroutines
Write coroutines with Flow, StateFlow, and error handling
Kotlin Testing
Write Android tests with JUnit, Mockk, and Espresso
Kotlin Coroutines & Flow
Use Kotlin Coroutines and Flow for reactive async streams
Android Room Database
Set up Room (SQLite) for local data persistence in Android Kotlin apps
Want a Kotlin / Android skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.