[{"data":1,"prerenderedAt":147},["ShallowReactive",2],{"public-settings":3,"post-jetpack-compose-state-hoisting":15,"article-code-1q1r8a8":146},{"site_title":4,"tagline":5,"site_url":6,"footer_text":5,"social_twitter":7,"social_github":7,"social_youtube":7,"social_linkedin":7,"social_mastodon":7,"og_image":8,"comments_enabled":9,"ads_mode":10,"ads_publisher_id":7,"ads_adtest":11,"ads_in_article_enabled":9,"ads_in_article_slot":7,"ads_in_article_paragraph":12,"ads_end_of_post_enabled":9,"ads_end_of_post_slot":7,"ads_feed_enabled":9,"ads_feed_slot":7,"ads_feed_every":13,"ads_sidebar_enabled":9,"ads_sidebar_slot":7,"ads_consent_enabled":11,"seo_enabled":9,"llms_enabled":9,"verification_google":7,"verification_bing":7,"performance_lcp_preload":9,"pwa_enabled":9,"bookmarks_enabled":9,"comments_moderation":9,"home_carousel_featured":9,"home_carousel_latest":9,"home_carousel_latest_mode":14,"home_carousel_series":9,"home_carousel_topics":9},"Android Learner","Practical Android development, explained clearly.","https:\u002F\u002Fwww.androidlearner.com","","\u002Fmedia\u002Fseed\u002Fcover-llm.webp","1","demo","0","2","3","latest",{"post":16,"comments":127},{"id":17,"title":18,"slug":19,"path":20,"excerpt":21,"cover_image":22,"cover_media_id":23,"status":24,"publish_at":25,"reading_time":26,"seo_title":27,"seo_description":21,"og_image":22,"canonical_url":28,"noindex":29,"featured":29,"faq":30,"howto":31,"review_note":31,"submitted_at":31,"reviewed_at":31,"category":32,"series":31,"tags":36,"author":45,"created_at":50,"updated_at":51,"body_md":52,"body_html":53,"related":54},2,"Jetpack Compose state hoisting","jetpack-compose-state-hoisting","\u002Fposts\u002Fjetpack-compose-state-hoisting","Keep Compose screens testable and recomposition cheap: state goes down, events go up, and the ViewModel stays the source of truth.","\u002Fmedia\u002Fseed\u002Fcover-compose.webp",6,"published","2026-11-17 03:14:13",3,"Jetpack Compose state hoisting, explained with code","https:\u002F\u002Fwww.androidlearner.com\u002Fposts\u002Fjetpack-compose-state-hoisting",false,[],null,{"id":17,"name":33,"slug":34,"description":35},"Jetpack Compose","jetpack-compose","Modern Android UI.",[37,41],{"id":38,"name":39,"slug":40,"description":31},4,"Compose","compose",{"id":42,"name":43,"slug":44,"description":31},5,"State hoisting","state-hoisting",{"id":46,"name":47,"username":48,"avatar":31,"profile_path":49},1,"Super Admin","superadmin","\u002Fu\u002Fsuperadmin","2026-11-20 05:18:17","2026-11-22 03:14:13","State hoisting is the Compose habit that keeps screens testable and recomposition cheap: **state goes down, events go up**. The child draws what it is told and asks the parent to change something. The parent (often a `ViewModel`) owns the source of truth.\n\n![State hoisting diagram](\u002Fmedia\u002Fseed\u002Finline-hoist.webp)\n\n## A widget that does not own its text\n\nA `TextField` that stores its own `var text by remember { mutableStateOf(\"\") }` is fine in a playground and awkward in a form. The parent cannot pre-fill it, clear it, or validate it without poking at the child.\n\n```kotlin title=\"SearchField.kt\" showLineNumbers\n@Composable\nfun SearchField(\n    query: String,\n    onQueryChange: (String) -> Unit,\n    modifier: Modifier = Modifier,\n) {\n    TextField(\n        value = query,\n        onValueChange = onQueryChange,\n        modifier = modifier,\n        placeholder = { Text(\"Search APIs\") },\n    )\n}\n```\n\nThe composable is now a **controlled** component. You can preview it with a hardcoded query, and you can drive it from a `ViewModel`.\n\n```kotlin\nclass SearchViewModel : ViewModel() {\n    var query by mutableStateOf(\"\")\n        private set\n\n    fun onQueryChange(value: String) {\n        query = value\n    }\n}\n\n@Composable\nfun SearchScreen(viewModel: SearchViewModel = viewModel()) {\n    SearchField(\n        query = viewModel.query,\n        onQueryChange = viewModel::onQueryChange,\n    )\n}\n```\n\n## Where state should live\n\nUse this rule of thumb, which matches the official Compose guidance:\n\n- **Ephemeral UI** (animation progress, a dropdown's expanded flag, a text field's cursor-related state that nobody else needs) can stay in the composable with `remember` or `rememberSaveable`.\n- **Screen state** that must survive rotation, process death, or be shared by siblings belongs in a `ViewModel`.\n- **App or session state** (the signed-in user, a repository cache) belongs lower, in a repository or data store, with the `ViewModel` as the screen's façade.\n\nHoist to the **lowest common ancestor that needs to read or write** the state — no higher. Hoisting everything to a god-sized `ViewModel` is how you end up with 40 parameters and a recomposition storm.\n\n## Events, not setters, at the boundary\n\nName parent callbacks after **what happened**, not after the field:\n\n```kotlin\ndata class EditorUiState(\n    val title: String = \"\",\n    val body: String = \"\",\n    val isSaving: Boolean = false,\n)\n\nsealed interface EditorEvent {\n    data class TitleChanged(val value: String) : EditorEvent\n    data class BodyChanged(val value: String) : EditorEvent\n    data object SaveClicked : EditorEvent\n}\n```\n\nThe screen becomes a function of `uiState` plus `onEvent`. That is easy to test: a screenshot test can pass a fake state, and a ViewModel test can feed events without a composition.\n\n## Avoiding wasted recomposition\n\nHoisting does not automatically make a list fast. Two habits help:\n\n1. **Read state as late as possible.** A parent that does `val query by viewModel.query.collectAsStateWithLifecycle()` and then passes `query` into a deep tree will recompose that tree on every keystroke. Sometimes that is fine. If a subtree does not need the query, do not pass it. `derivedStateOf` is useful when you derive something cheaper (for example \"the save button is enabled\") from a frequently changing input.\n2. **Prefer `collectAsStateWithLifecycle()`** over `collectAsState()` in screens that leave the composition while the process stays alive, so you do not keep collectors hot on a stopped screen.\n\n```kotlin\nval uiState by viewModel.uiState.collectAsStateWithLifecycle()\n```\n\n## A compact example\n\n```kotlin\n@Composable\nfun Counter(\n    count: Int,\n    onIncrement: () -> Unit,\n    modifier: Modifier = Modifier,\n) {\n    Row(modifier) {\n        Text(\"Count: $count\")\n        Button(onClick = onIncrement) { Text(\"Add\") }\n    }\n}\n```\n\nThe test does not need a `ViewModel`. It can call `Counter(count = 2, onIncrement = {})` and assert the label. The `ViewModel` test can increment a `MutableStateFlow` and assert the new value. That split is the whole point of hoisting: **UI is a function of state; state changes are events.**\n","\u003Cp>State hoisting is the Compose habit that keeps screens testable and recomposition cheap: \u003Cstrong>state goes down, events go up\u003C\u002Fstrong>. The child draws what it is told and asks the parent to change something. The parent (often a \u003Ccode>ViewModel\u003C\u002Fcode>) owns the source of truth.\u003C\u002Fp>\n\u003Cp>\u003Cimg src=\"\u002Fmedia\u002Fseed\u002Finline-hoist.webp\" alt=\"State hoisting diagram\" \u002F>\u003C\u002Fp>\n\u003Ch2 id=\"a-widget-that-does-not-own-its-text\">A widget that does not own its text\u003C\u002Fh2>\n\u003Cp>A \u003Ccode>TextField\u003C\u002Fcode> that stores its own \u003Ccode>var text by remember { mutableStateOf(&quot;&quot;) }\u003C\u002Fcode> is fine in a playground and awkward in a form. The parent cannot pre-fill it, clear it, or validate it without poking at the child.\u003C\u002Fp>\n\u003Cpre class=\"code-block\" data-lang=\"kotlin\" data-filename=\"SearchField.kt\" data-line-numbers=\"true\">\u003Ccode class=\"language-kotlin\">@Composable\nfun SearchField(\n    query: String,\n    onQueryChange: (String) -&gt; Unit,\n    modifier: Modifier = Modifier,\n) {\n    TextField(\n        value = query,\n        onValueChange = onQueryChange,\n        modifier = modifier,\n        placeholder = { Text(&quot;Search APIs&quot;) },\n    )\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>The composable is now a \u003Cstrong>controlled\u003C\u002Fstrong> component. You can preview it with a hardcoded query, and you can drive it from a \u003Ccode>ViewModel\u003C\u002Fcode>.\u003C\u002Fp>\n\u003Cpre class=\"code-block\" data-lang=\"kotlin\">\u003Ccode class=\"language-kotlin\">class SearchViewModel : ViewModel() {\n    var query by mutableStateOf(&quot;&quot;)\n        private set\n\n    fun onQueryChange(value: String) {\n        query = value\n    }\n}\n\n@Composable\nfun SearchScreen(viewModel: SearchViewModel = viewModel()) {\n    SearchField(\n        query = viewModel.query,\n        onQueryChange = viewModel::onQueryChange,\n    )\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2 id=\"where-state-should-live\">Where state should live\u003C\u002Fh2>\n\u003Cp>Use this rule of thumb, which matches the official Compose guidance:\u003C\u002Fp>\n\u003Cul>\n\u003Cli>\u003Cstrong>Ephemeral UI\u003C\u002Fstrong> (animation progress, a dropdown's expanded flag, a text field's cursor-related state that nobody else needs) can stay in the composable with \u003Ccode>remember\u003C\u002Fcode> or \u003Ccode>rememberSaveable\u003C\u002Fcode>.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Screen state\u003C\u002Fstrong> that must survive rotation, process death, or be shared by siblings belongs in a \u003Ccode>ViewModel\u003C\u002Fcode>.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>App or session state\u003C\u002Fstrong> (the signed-in user, a repository cache) belongs lower, in a repository or data store, with the \u003Ccode>ViewModel\u003C\u002Fcode> as the screen's façade.\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>Hoist to the \u003Cstrong>lowest common ancestor that needs to read or write\u003C\u002Fstrong> the state — no higher. Hoisting everything to a god-sized \u003Ccode>ViewModel\u003C\u002Fcode> is how you end up with 40 parameters and a recomposition storm.\u003C\u002Fp>\n\u003Ch2 id=\"events-not-setters-at-the-boundary\">Events, not setters, at the boundary\u003C\u002Fh2>\n\u003Cp>Name parent callbacks after \u003Cstrong>what happened\u003C\u002Fstrong>, not after the field:\u003C\u002Fp>\n\u003Cpre class=\"code-block\" data-lang=\"kotlin\">\u003Ccode class=\"language-kotlin\">data class EditorUiState(\n    val title: String = &quot;&quot;,\n    val body: String = &quot;&quot;,\n    val isSaving: Boolean = false,\n)\n\nsealed interface EditorEvent {\n    data class TitleChanged(val value: String) : EditorEvent\n    data class BodyChanged(val value: String) : EditorEvent\n    data object SaveClicked : EditorEvent\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>The screen becomes a function of \u003Ccode>uiState\u003C\u002Fcode> plus \u003Ccode>onEvent\u003C\u002Fcode>. That is easy to test: a screenshot test can pass a fake state, and a ViewModel test can feed events without a composition.\u003C\u002Fp>\n\u003Ch2 id=\"avoiding-wasted-recomposition\">Avoiding wasted recomposition\u003C\u002Fh2>\n\u003Cp>Hoisting does not automatically make a list fast. Two habits help:\u003C\u002Fp>\n\u003Col>\n\u003Cli>\u003Cstrong>Read state as late as possible.\u003C\u002Fstrong> A parent that does \u003Ccode>val query by viewModel.query.collectAsStateWithLifecycle()\u003C\u002Fcode> and then passes \u003Ccode>query\u003C\u002Fcode> into a deep tree will recompose that tree on every keystroke. Sometimes that is fine. If a subtree does not need the query, do not pass it. \u003Ccode>derivedStateOf\u003C\u002Fcode> is useful when you derive something cheaper (for example &quot;the save button is enabled&quot;) from a frequently changing input.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Prefer \u003Ccode>collectAsStateWithLifecycle()\u003C\u002Fcode>\u003C\u002Fstrong> over \u003Ccode>collectAsState()\u003C\u002Fcode> in screens that leave the composition while the process stays alive, so you do not keep collectors hot on a stopped screen.\u003C\u002Fli>\n\u003C\u002Fol>\n\u003Cpre class=\"code-block\" data-lang=\"kotlin\">\u003Ccode class=\"language-kotlin\">val uiState by viewModel.uiState.collectAsStateWithLifecycle()\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2 id=\"a-compact-example\">A compact example\u003C\u002Fh2>\n\u003Cpre class=\"code-block\" data-lang=\"kotlin\">\u003Ccode class=\"language-kotlin\">@Composable\nfun Counter(\n    count: Int,\n    onIncrement: () -&gt; Unit,\n    modifier: Modifier = Modifier,\n) {\n    Row(modifier) {\n        Text(&quot;Count: $count&quot;)\n        Button(onClick = onIncrement) { Text(&quot;Add&quot;) }\n    }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>The test does not need a \u003Ccode>ViewModel\u003C\u002Fcode>. It can call \u003Ccode>Counter(count = 2, onIncrement = {})\u003C\u002Fcode> and assert the label. The \u003Ccode>ViewModel\u003C\u002Fcode> test can increment a \u003Ccode>MutableStateFlow\u003C\u002Fcode> and assert the new value. That split is the whole point of hoisting: \u003Cstrong>UI is a function of state; state changes are events.\u003C\u002Fstrong>\u003C\u002Fp>\n",[55,91],{"id":56,"title":57,"slug":58,"path":59,"excerpt":60,"cover_image":61,"cover_media_id":62,"status":24,"publish_at":63,"reading_time":64,"seo_title":65,"seo_description":66,"og_image":61,"canonical_url":67,"noindex":29,"featured":29,"faq":68,"howto":31,"review_note":31,"submitted_at":31,"reviewed_at":31,"category":75,"series":31,"tags":76,"author":88,"created_at":89,"updated_at":90},20,"Material 2 Is Deprecated: Move to Material 3 1.5 and TextFieldState","migrate-compose-material-2-to-material-3-1-5","\u002Fposts\u002Fmigrate-compose-material-2-to-material-3-1-5","Material 3 1.5.0 is stable and Material 2 is no longer actively maintained. This guide covers the theme swap, component move, and TextFieldState migration.","\u002Fmedia\u002F2026\u002F11\u002Faf355ed5e362f80d.webp",57,"2026-11-25 01:00:00",8,"Migrate from Compose Material 2 to Material 3 1.5","Compose Material 2 is no longer maintained. Adopt Material 3 1.5.0 and replace deprecated value-based TextField APIs with TextFieldState.","https:\u002F\u002Fwww.androidlearner.com\u002Fposts\u002Fmigrate-compose-material-2-to-material-3-1-5",[69,72],{"question":70,"answer":71},"Is Material 2 removed from the Compose BOM?","No. It is deprecated and no longer actively maintained, but existing Material 2 screens still compile. Plan the migration if you want ongoing bug fixes and the current design language.",{"question":73,"answer":74},"Are Material 3 value-based TextField overloads deprecated in 1.5.0?","No. Android Developers say those Material 3 overloads are not deprecated yet in 1.5.0, but they already recommend the state-based TextField and OutlinedTextField APIs. Value-based BasicTextField and Material 2 TextField are deprecated in Compose 1.13.",{"id":17,"name":33,"slug":34,"description":35},[77,81,82,84],{"id":78,"name":79,"slug":80,"description":31},28,"Android","android",{"id":38,"name":39,"slug":40,"description":31},{"id":83,"name":33,"slug":34,"description":31},18,{"id":85,"name":86,"slug":87,"description":31},46,"Material 3","material-3",{"id":46,"name":47,"username":48,"avatar":31,"profile_path":49},"2026-11-24 19:51:49","2026-11-24 19:53:11",{"id":92,"title":93,"slug":94,"path":95,"excerpt":96,"cover_image":97,"cover_media_id":98,"status":24,"publish_at":99,"reading_time":100,"seo_title":101,"seo_description":102,"og_image":97,"canonical_url":103,"noindex":29,"featured":29,"faq":104,"howto":31,"review_note":31,"submitted_at":31,"reviewed_at":31,"category":111,"series":31,"tags":112,"author":124,"created_at":125,"updated_at":126},19,"What's New in Jetpack Compose 1.13 (November 2026)","jetpack-compose-1-13-flexbox-grid-progressive-blur","\u002Fposts\u002Fjetpack-compose-1-13-flexbox-grid-progressive-blur","Compose 1.13 is stable in BOM 2026.11.01. Here is how to upgrade, when to use FlexBox and Grid, and what progressive blur and the new deprecations actually mean.","\u002Fmedia\u002F2026\u002F11\u002F6fab1724acc6e8e3.webp",54,"2026-11-25 00:30:00",7,"Jetpack Compose 1.13: FlexBox, Grid, and Progressive Blur","Upgrade to Compose BOM 2026.11.01. Stable FlexBox and Grid, progressive blur, PolygonShape, and the Kotlin 2.2 \u002F compileSdk 37.1 requirements.","https:\u002F\u002Fwww.androidlearner.com\u002Fposts\u002Fjetpack-compose-1-13-flexbox-grid-progressive-blur",[105,108],{"question":106,"answer":107},"Do I need Kotlin 2.2 to use Compose 1.13?","Yes. The official 18 November 2026 Compose release post requires Kotlin 2.2 or newer and compileSdk 37.1 when you upgrade to Compose BOM 2026.11.01.",{"question":109,"answer":110},"Should I replace FlowRow and FlowColumn immediately?","Not necessarily. Android Developers say FlexBox supersedes the Flow layouts and recommend FlexBox for new code. Existing Flow call sites still compile; move them when you next touch those screens.",{"id":17,"name":33,"slug":34,"description":35},[113,114,115,119,123],{"id":78,"name":79,"slug":80,"description":31},{"id":38,"name":39,"slug":40,"description":31},{"id":116,"name":117,"slug":118,"description":31},44,"FlexBox","flexbox",{"id":120,"name":121,"slug":122,"description":31},45,"Grid","grid",{"id":83,"name":33,"slug":34,"description":31},{"id":46,"name":47,"username":48,"avatar":31,"profile_path":49},"2026-11-24 19:51:47","2026-11-24 19:53:09",[128,140],{"id":129,"parent_id":31,"author_name":130,"body":131,"created_at":132,"replies":133},79,"Lena Ortiz","This finally clicked when you pulled the checkbox state out of the row composable. Our lists stopped recomposing the whole screen.","2026-11-18 09:14:13",[134],{"id":135,"parent_id":129,"author_name":136,"body":137,"created_at":138,"replies":139},80,"Jordan Lee","Same here. We also hoisted the click lambda and passed a stable callback from the ViewModel; traces got a lot quieter.","2026-11-18 18:14:13",[],{"id":141,"parent_id":31,"author_name":142,"body":143,"created_at":144,"replies":145},81,"Chen Wei","One thing I would add: key LazyColumn items by id, not index, once the state lives higher up. Otherwise you get the classic wrong-row-checked bug.","2026-11-20 11:14:13",[],"\u003Cp>State hoisting is the Compose habit that keeps screens testable and recomposition cheap: \u003Cstrong>state goes down, events go up\u003C\u002Fstrong>. The child draws what it is told and asks the parent to change something. The parent (often a \u003Ccode>ViewModel\u003C\u002Fcode>) owns the source of truth.\u003C\u002Fp>\n\u003Cp>\u003Cimg src=\"\u002Fmedia\u002Fseed\u002Finline-hoist.webp\" alt=\"State hoisting diagram\" \u002F>\u003C\u002Fp>\n\u003Ch2 id=\"a-widget-that-does-not-own-its-text\">A widget that does not own its text\u003C\u002Fh2>\n\u003Cp>A \u003Ccode>TextField\u003C\u002Fcode> that stores its own \u003Ccode>var text by remember { mutableStateOf(&quot;&quot;) }\u003C\u002Fcode> is fine in a playground and awkward in a form. The parent cannot pre-fill it, clear it, or validate it without poking at the child.\u003C\u002Fp>\n\u003Cdiv class=\"code-block code-block--file code-block--numbered\" data-lang=\"kotlin\" data-filename=\"SearchField.kt\" data-line-numbers=\"true\">\u003Cdiv class=\"code-block__header\">\u003Cspan class=\"code-block__filename\">SearchField.kt\u003C\u002Fspan>\u003Cspan class=\"code-block__lang\">kotlin\u003C\u002Fspan>\u003Cbutton type=\"button\" class=\"code-block__copy\" aria-label=\"Copy code\">Copy\u003C\u002Fbutton>\u003C\u002Fdiv>\u003Cdiv class=\"code-block__body\">\u003Cpre class=\"shiki github-dark\" style=\"background-color:#24292e;color:#e1e4e8\" tabindex=\"0\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:#B392F0\">@Composable\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">fun\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> SearchField\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">    query: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">String\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">,\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">    onQueryChange: (\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">String\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">) -> \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">Unit\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">,\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">    modifier: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">Modifier\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> =\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> Modifier,\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#B392F0\">    TextField\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        value\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> =\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> query,\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        onValueChange \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> onQueryChange,\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        modifier \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> modifier,\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        placeholder \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> { \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">Text\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"Search APIs\"\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">) },\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">    )\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">}\u003C\u002Fspan>\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>The composable is now a \u003Cstrong>controlled\u003C\u002Fstrong> component. You can preview it with a hardcoded query, and you can drive it from a \u003Ccode>ViewModel\u003C\u002Fcode>.\u003C\u002Fp>\n\u003Cdiv class=\"code-block\" data-lang=\"kotlin\">\u003Cdiv class=\"code-block__header\">\u003Cspan class=\"code-block__lang\">kotlin\u003C\u002Fspan>\u003Cbutton type=\"button\" class=\"code-block__copy\" aria-label=\"Copy code\">Copy\u003C\u002Fbutton>\u003C\u002Fdiv>\u003Cdiv class=\"code-block__body\">\u003Cpre class=\"shiki github-dark\" style=\"background-color:#24292e;color:#e1e4e8\" tabindex=\"0\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">class\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> SearchViewModel\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> : \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">ViewModel\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">() {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">    var\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> query \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">by\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> mutableStateOf\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"\"\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">)\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        private\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> set\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">    fun\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> onQueryChange\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">value\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">String\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        query \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> value\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">    }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">}\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#B392F0\">@Composable\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">fun\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> SearchScreen\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(viewModel: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">SearchViewModel\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> =\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> viewModel\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">()) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#B392F0\">    SearchField\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        query \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> viewModel.query,\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        onQueryChange \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> viewModel::\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">onQueryChange\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">,\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">    )\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">}\u003C\u002Fspan>\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Ch2 id=\"where-state-should-live\">Where state should live\u003C\u002Fh2>\n\u003Cp>Use this rule of thumb, which matches the official Compose guidance:\u003C\u002Fp>\n\u003Cul>\n\u003Cli>\u003Cstrong>Ephemeral UI\u003C\u002Fstrong> (animation progress, a dropdown's expanded flag, a text field's cursor-related state that nobody else needs) can stay in the composable with \u003Ccode>remember\u003C\u002Fcode> or \u003Ccode>rememberSaveable\u003C\u002Fcode>.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Screen state\u003C\u002Fstrong> that must survive rotation, process death, or be shared by siblings belongs in a \u003Ccode>ViewModel\u003C\u002Fcode>.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>App or session state\u003C\u002Fstrong> (the signed-in user, a repository cache) belongs lower, in a repository or data store, with the \u003Ccode>ViewModel\u003C\u002Fcode> as the screen's façade.\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>Hoist to the \u003Cstrong>lowest common ancestor that needs to read or write\u003C\u002Fstrong> the state — no higher. Hoisting everything to a god-sized \u003Ccode>ViewModel\u003C\u002Fcode> is how you end up with 40 parameters and a recomposition storm.\u003C\u002Fp>\n\u003Ch2 id=\"events-not-setters-at-the-boundary\">Events, not setters, at the boundary\u003C\u002Fh2>\n\u003Cp>Name parent callbacks after \u003Cstrong>what happened\u003C\u002Fstrong>, not after the field:\u003C\u002Fp>\n\u003Cdiv class=\"code-block\" data-lang=\"kotlin\">\u003Cdiv class=\"code-block__header\">\u003Cspan class=\"code-block__lang\">kotlin\u003C\u002Fspan>\u003Cbutton type=\"button\" class=\"code-block__copy\" aria-label=\"Copy code\">Copy\u003C\u002Fbutton>\u003C\u002Fdiv>\u003Cdiv class=\"code-block__body\">\u003Cpre class=\"shiki github-dark\" style=\"background-color:#24292e;color:#e1e4e8\" tabindex=\"0\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">data\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> class\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> EditorUiState\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">    val\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> title: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">String\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> =\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\"> \"\"\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">,\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">    val\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> body: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">String\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> =\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\"> \"\"\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">,\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">    val\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> isSaving: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">Boolean\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> =\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\"> false\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">,\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">)\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">sealed\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> interface\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> EditorEvent\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">    data\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> class\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> TitleChanged\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">val\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> value\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">String\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">) : \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">EditorEvent\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">    data\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> class\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> BodyChanged\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">val\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> value\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">String\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">) : \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">EditorEvent\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">    data\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> object\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> SaveClicked\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> : \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">EditorEvent\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">}\u003C\u002Fspan>\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>The screen becomes a function of \u003Ccode>uiState\u003C\u002Fcode> plus \u003Ccode>onEvent\u003C\u002Fcode>. That is easy to test: a screenshot test can pass a fake state, and a ViewModel test can feed events without a composition.\u003C\u002Fp>\n\u003Ch2 id=\"avoiding-wasted-recomposition\">Avoiding wasted recomposition\u003C\u002Fh2>\n\u003Cp>Hoisting does not automatically make a list fast. Two habits help:\u003C\u002Fp>\n\u003Col>\n\u003Cli>\u003Cstrong>Read state as late as possible.\u003C\u002Fstrong> A parent that does \u003Ccode>val query by viewModel.query.collectAsStateWithLifecycle()\u003C\u002Fcode> and then passes \u003Ccode>query\u003C\u002Fcode> into a deep tree will recompose that tree on every keystroke. Sometimes that is fine. If a subtree does not need the query, do not pass it. \u003Ccode>derivedStateOf\u003C\u002Fcode> is useful when you derive something cheaper (for example &quot;the save button is enabled&quot;) from a frequently changing input.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Prefer \u003Ccode>collectAsStateWithLifecycle()\u003C\u002Fcode>\u003C\u002Fstrong> over \u003Ccode>collectAsState()\u003C\u002Fcode> in screens that leave the composition while the process stays alive, so you do not keep collectors hot on a stopped screen.\u003C\u002Fli>\n\u003C\u002Fol>\n\u003Cdiv class=\"code-block\" data-lang=\"kotlin\">\u003Cdiv class=\"code-block__header\">\u003Cspan class=\"code-block__lang\">kotlin\u003C\u002Fspan>\u003Cbutton type=\"button\" class=\"code-block__copy\" aria-label=\"Copy code\">Copy\u003C\u002Fbutton>\u003C\u002Fdiv>\u003Cdiv class=\"code-block__body\">\u003Cpre class=\"shiki github-dark\" style=\"background-color:#24292e;color:#e1e4e8\" tabindex=\"0\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">val\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> uiState \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">by\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> viewModel.uiState.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">collectAsStateWithLifecycle\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">()\u003C\u002Fspan>\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Ch2 id=\"a-compact-example\">A compact example\u003C\u002Fh2>\n\u003Cdiv class=\"code-block\" data-lang=\"kotlin\">\u003Cdiv class=\"code-block__header\">\u003Cspan class=\"code-block__lang\">kotlin\u003C\u002Fspan>\u003Cbutton type=\"button\" class=\"code-block__copy\" aria-label=\"Copy code\">Copy\u003C\u002Fbutton>\u003C\u002Fdiv>\u003Cdiv class=\"code-block__body\">\u003Cpre class=\"shiki github-dark\" style=\"background-color:#24292e;color:#e1e4e8\" tabindex=\"0\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:#B392F0\">@Composable\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">fun\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> Counter\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">    count: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">Int\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">,\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">    onIncrement: () \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">->\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> Unit,\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">    modifier: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">Modifier\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> =\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> Modifier,\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#B392F0\">    Row\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(modifier) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#B392F0\">        Text\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"Count: \u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\">$count\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">)\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#B392F0\">        Button\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(onClick \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> onIncrement) { \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">Text\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"Add\"\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">) }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">    }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">}\u003C\u002Fspan>\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>The test does not need a \u003Ccode>ViewModel\u003C\u002Fcode>. It can call \u003Ccode>Counter(count = 2, onIncrement = {})\u003C\u002Fcode> and assert the label. The \u003Ccode>ViewModel\u003C\u002Fcode> test can increment a \u003Ccode>MutableStateFlow\u003C\u002Fcode> and assert the new value. That split is the whole point of hoisting: \u003Cstrong>UI is a function of state; state changes are events.\u003C\u002Fstrong>\u003C\u002Fp>\n",1795550404300]