How to build a new Compose screen with state hoisting
何时使用: New screen in a Compose app — get it right the first time rather than refactoring later.
前置条件
- 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
步骤
-
Describe the screenBuild a Profile screen: avatar, name, bio, follow button. State comes from a ProfileViewModel.✓ 已复制→ Stateless composable + stateful wrapper + preview
-
Wire the ViewModelHook up the ViewModel using collectAsStateWithLifecycle.✓ 已复制→ Lifecycle-aware state collection
-
Add a previewAdd @Preview variants for empty, loaded, and error states.✓ 已复制→ Three previews rendering without a live ViewModel
结果: A clean Compose screen with previews and a testable structure.
注意事项
- 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.