How to build a new Compose screen with state hoisting
When to use: New screen in a Compose app — get it right the first time rather than refactoring later.
Prerequisites
- Android Studio with a recent Compose BOM — Use the latest stable Compose BOM in your build
- Skill cloned — git clone https://github.com/anhvt52/jetpack-compose-skills ~/.claude/skills/jetpack-compose-skills
Flow
-
Describe the screenBuild a Profile screen: avatar, name, bio, follow button. State comes from a ProfileViewModel.✓ Copied→ Stateless composable + stateful wrapper + preview
-
Wire the ViewModelHook up the ViewModel using collectAsStateWithLifecycle.✓ Copied→ Lifecycle-aware state collection
-
Add a previewAdd @Preview variants for empty, loaded, and error states.✓ Copied→ Three previews rendering without a live ViewModel
Outcome: A clean Compose screen with previews and a testable structure.
Pitfalls
- Stateful composables you can't preview — Keep the composable stateless; hoist state to a wrapper
- Wrong Modifier order — Skill prompts the order rule — padding before background for a padded background, etc.