[{"data":1,"prerenderedAt":231},["ShallowReactive",2],{"public-settings":3,"footer-custom-pages":18,"post-jetpack-appstate-snapshot-state-architecture":31,"article-code-1d20i7g":230},{"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":11,"ads_adtest":12,"ads_in_article_enabled":9,"ads_in_article_slot":13,"ads_in_article_paragraph":14,"ads_end_of_post_enabled":9,"ads_end_of_post_slot":13,"ads_feed_enabled":9,"ads_feed_slot":13,"ads_feed_every":15,"ads_sidebar_enabled":9,"ads_sidebar_slot":13,"ads_consent_enabled":9,"clarity_enabled":12,"clarity_project_id":16,"seo_enabled":9,"llms_enabled":9,"verification_google":7,"verification_bing":7,"seo_home_title":7,"seo_description":7,"indexnow_key":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":17,"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","live","ca-pub-3394535770235553","0","9901451769","2","3","jmrmm96ef6","latest",[19,26],{"id":20,"title":21,"slug":22,"show_in_footer":23,"sort_order":24,"updated_at":25},1,"Terms & Conditions","terms",true,10,"2026-11-28 13:13:48",{"id":27,"title":28,"slug":29,"show_in_footer":23,"sort_order":30,"updated_at":25},2,"Privacy Policy","privacy",20,{"post":32,"comments":229},{"id":33,"title":34,"slug":35,"path":36,"excerpt":37,"cover_image":38,"cover_media_id":39,"status":40,"publish_at":41,"reading_time":27,"seo_title":42,"seo_description":43,"og_image":38,"canonical_url":44,"noindex":45,"featured":45,"faq":46,"howto":53,"review_note":53,"submitted_at":53,"reviewed_at":53,"category":54,"series":53,"tags":59,"author":78,"created_at":81,"updated_at":82,"body_md":83,"body_html":84,"related":85},27,"Jetpack AppState & Snapshot State Architecture: Beyond UI State","jetpack-appstate-snapshot-state-architecture","\u002Fposts\u002Fjetpack-appstate-snapshot-state-architecture","Compose's snapshot state system isn't just for UI. Learn how Jetpack AppState and snapshot-backed stores synchronize reactive state across screens, services, and widgets.","\u002Fmedia\u002F2026\u002F11\u002F3a2381e9f25fce93.webp",69,"published","2026-11-28 15:43:00","Jetpack AppState & Snapshot State Architecture in Android","Use Compose's snapshot state system and Jetpack AppState patterns to manage reactive state cleanly across ViewModels, background workers, and widgets.","https:\u002F\u002Fwww.androidlearner.com\u002Fposts\u002Fjetpack-appstate-snapshot-state-architecture\u002F",false,[47,50],{"question":48,"answer":49},"How is snapshot state different from MutableStateFlow?","Snapshot state tracks fine-grained property reads automatically without explicit combine\u002Fmap operators, and supports atomic multi-property transactions via Snapshot.withMutableSnapshot.",{"question":51,"answer":52},"Can snapshot state be used outside @Composable functions?","Yes. Compose Runtime's snapshot system has no Android View or UI dependency and can run in ViewModels, repositories, or background workers using snapshotFlow.",null,{"id":55,"name":56,"slug":57,"description":58},4,"Architecture","architecture","App architecture and data layers.",[60,64,66,70,74],{"id":61,"name":62,"slug":63,"description":53},28,"Android","android",{"id":65,"name":56,"slug":57,"description":53},48,{"id":67,"name":68,"slug":69,"description":53},24,"Jetpack","jetpack",{"id":71,"name":72,"slug":73,"description":53},18,"Jetpack Compose","jetpack-compose",{"id":75,"name":76,"slug":77,"description":53},5,"State hoisting","state-hoisting",{"id":20,"name":4,"username":79,"avatar":53,"profile_path":80},"superadmin","\u002Fu\u002Fsuperadmin","2026-11-28 10:11:54","2026-11-28 10:14:38","Most Android developers know `mutableStateOf` and `derivedStateOf` as tools inside `@Composable` functions. Fewer realize that Compose's **snapshot state system** is a standalone, thread-safe transactional state engine—and the foundation behind **Jetpack AppState** patterns.\n\n## Why Snapshot State Works Outside the UI\n\nTraditional `StateFlow` pipelines often require verbose `combine(...)` chains when multiple slices of state depend on each other. Snapshot state solves this with **automatic dependency tracking** and **atomic transactions**:\n\n- **Fine-grained reads**: Consumers only re-evaluate when the exact fields they read actually change.\n- **Atomic updates**: `Snapshot.withMutableSnapshot` updates multiple fields together so observers never see a half-updated state.\n- **Zero UI coupling**: `androidx.compose.runtime` works in pure Kotlin modules, `ViewModel`s, and services.\n\n## Building a Snapshot-Backed State Store\n\nYou can encapsulate domain state in a plain Kotlin class backed by `mutableStateOf` and `derivedStateOf`:\n\n```kotlin\nclass CartStateStore {\n    var items by mutableStateOf\u003CList\u003CCartItem>>(emptyList())\n        private set\n\n    var promoDiscountPercent by mutableStateOf(0)\n        private set\n\n    val totalCents: Int by derivedStateOf {\n        val subtotal = items.sumOf { it.priceCents * it.quantity }\n        subtotal - (subtotal * promoDiscountPercent \u002F 100)\n    }\n\n    fun applyBundle(newItems: List\u003CCartItem>, discount: Int) {\n        Snapshot.withMutableSnapshot {\n            items = newItems\n            promoDiscountPercent = discount\n        }\n    }\n}\n```\n\nBecause `applyBundle` runs inside `Snapshot.withMutableSnapshot`, `totalCents` and any UI reading `items` + `promoDiscountPercent` observe both changes atomically.\n\n## Bridging Snapshot State to Coroutines &amp; Widgets\n\nWhen non-Compose consumers (like a `WorkManager` sync job, analytics logger, or Glance widget updater) need a cold stream of changes, use `snapshotFlow`:\n\n```kotlin\nclass CartSyncObserver(\n    private val store: CartStateStore,\n    private val widgetUpdater: WidgetUpdater\n) {\n    suspend fun observeAndSync() {\n        snapshotFlow { store.items.size to store.totalCents }\n            .distinctUntilChanged()\n            .collect { (count, total) ->\n                widgetUpdater.updateCartBadge(itemCount = count, totalCents = total)\n            }\n    }\n}\n```\n\n## When to Use Snapshot State vs. `StateFlow`\n\n- **Use `StateFlow` \u002F `Flow`** for asynchronous I\u002FO streams (Room queries, WebSockets, Ktor responses).\n- **Use Snapshot State \u002F AppState** for synchronous, interconnected UI and session state where derived values and atomic multi-field updates matter.\n\n","\u003Cp>Most Android developers know \u003Ccode>mutableStateOf\u003C\u002Fcode> and \u003Ccode>derivedStateOf\u003C\u002Fcode> as tools inside \u003Ccode>@Composable\u003C\u002Fcode> functions. Fewer realize that Compose's \u003Cstrong>snapshot state system\u003C\u002Fstrong> is a standalone, thread-safe transactional state engine—and the foundation behind \u003Cstrong>Jetpack AppState\u003C\u002Fstrong> patterns.\u003C\u002Fp>\n\u003Ch2 id=\"why-snapshot-state-works-outside-the-ui\">Why Snapshot State Works Outside the UI\u003C\u002Fh2>\n\u003Cp>Traditional \u003Ccode>StateFlow\u003C\u002Fcode> pipelines often require verbose \u003Ccode>combine(...)\u003C\u002Fcode> chains when multiple slices of state depend on each other. Snapshot state solves this with \u003Cstrong>automatic dependency tracking\u003C\u002Fstrong> and \u003Cstrong>atomic transactions\u003C\u002Fstrong>:\u003C\u002Fp>\n\u003Cul>\n\u003Cli>\u003Cstrong>Fine-grained reads\u003C\u002Fstrong>: Consumers only re-evaluate when the exact fields they read actually change.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Atomic updates\u003C\u002Fstrong>: \u003Ccode>Snapshot.withMutableSnapshot\u003C\u002Fcode> updates multiple fields together so observers never see a half-updated state.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Zero UI coupling\u003C\u002Fstrong>: \u003Ccode>androidx.compose.runtime\u003C\u002Fcode> works in pure Kotlin modules, \u003Ccode>ViewModel\u003C\u002Fcode>s, and services.\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Ch2 id=\"building-a-snapshot-backed-state-store\">Building a Snapshot-Backed State Store\u003C\u002Fh2>\n\u003Cp>You can encapsulate domain state in a plain Kotlin class backed by \u003Ccode>mutableStateOf\u003C\u002Fcode> and \u003Ccode>derivedStateOf\u003C\u002Fcode>:\u003C\u002Fp>\n\u003Cpre class=\"code-block\" data-lang=\"kotlin\">\u003Ccode class=\"language-kotlin\">class CartStateStore {\n    var items by mutableStateOf&lt;List&lt;CartItem&gt;&gt;(emptyList())\n        private set\n\n    var promoDiscountPercent by mutableStateOf(0)\n        private set\n\n    val totalCents: Int by derivedStateOf {\n        val subtotal = items.sumOf { it.priceCents * it.quantity }\n        subtotal - (subtotal * promoDiscountPercent \u002F 100)\n    }\n\n    fun applyBundle(newItems: List&lt;CartItem&gt;, discount: Int) {\n        Snapshot.withMutableSnapshot {\n            items = newItems\n            promoDiscountPercent = discount\n        }\n    }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>Because \u003Ccode>applyBundle\u003C\u002Fcode> runs inside \u003Ccode>Snapshot.withMutableSnapshot\u003C\u002Fcode>, \u003Ccode>totalCents\u003C\u002Fcode> and any UI reading \u003Ccode>items\u003C\u002Fcode> + \u003Ccode>promoDiscountPercent\u003C\u002Fcode> observe both changes atomically.\u003C\u002Fp>\n\u003Ch2 id=\"bridging-snapshot-state-to-coroutines-widgets\">Bridging Snapshot State to Coroutines &amp; Widgets\u003C\u002Fh2>\n\u003Cp>When non-Compose consumers (like a \u003Ccode>WorkManager\u003C\u002Fcode> sync job, analytics logger, or Glance widget updater) need a cold stream of changes, use \u003Ccode>snapshotFlow\u003C\u002Fcode>:\u003C\u002Fp>\n\u003Cpre class=\"code-block\" data-lang=\"kotlin\">\u003Ccode class=\"language-kotlin\">class CartSyncObserver(\n    private val store: CartStateStore,\n    private val widgetUpdater: WidgetUpdater\n) {\n    suspend fun observeAndSync() {\n        snapshotFlow { store.items.size to store.totalCents }\n            .distinctUntilChanged()\n            .collect { (count, total) -&gt;\n                widgetUpdater.updateCartBadge(itemCount = count, totalCents = total)\n            }\n    }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Ch2 id=\"when-to-use-snapshot-state-vs-stateflow\">When to Use Snapshot State vs. \u003Ccode>StateFlow\u003C\u002Fcode>\u003C\u002Fh2>\n\u003Cul>\n\u003Cli>\u003Cstrong>Use \u003Ccode>StateFlow\u003C\u002Fcode> \u002F \u003Ccode>Flow\u003C\u002Fcode>\u003C\u002Fstrong> for asynchronous I\u002FO streams (Room queries, WebSockets, Ktor responses).\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Use Snapshot State \u002F AppState\u003C\u002Fstrong> for synchronous, interconnected UI and session state where derived values and atomic multi-field updates matter.\u003C\u002Fli>\n\u003C\u002Ful>\n",[86,127,158,190],{"id":87,"title":88,"slug":89,"path":90,"excerpt":91,"cover_image":92,"cover_media_id":93,"status":40,"publish_at":94,"reading_time":27,"seo_title":95,"seo_description":96,"og_image":92,"canonical_url":97,"noindex":45,"featured":45,"faq":98,"howto":53,"review_note":53,"submitted_at":53,"reviewed_at":53,"category":105,"series":53,"tags":109,"author":124,"created_at":125,"updated_at":126},23,"Android 17 AppFunctions & Android MCP: Exposing App Actions to Gemini","android-17-appfunctions-android-mcp-gemini","\u002Fposts\u002Fandroid-17-appfunctions-android-mcp-gemini","Android 17 expands AppFunctions so apps can expose structured actions to Gemini and Android MCP clients. Learn how to declare functions, validate inputs, and test locally.","\u002Fmedia\u002F2026\u002F11\u002F89e9aba2e88d1684.webp",65,"2026-11-28 15:45:00","Android 17 AppFunctions & Android MCP Guide","Learn how to expose structured in-app capabilities using Android 17 AppFunctions and Jetpack libraries for Gemini and Android MCP orchestration.","https:\u002F\u002Fwww.androidlearner.com\u002Fposts\u002Fandroid-17-appfunctions-android-mcp-gemini\u002F",[99,102],{"question":100,"answer":101},"Do AppFunctions replace deep links or App Actions?","No. Deep links still open specific screens from URLs or intents, while AppFunctions expose typed, callable capabilities that the system or an on-device assistant can invoke with structured parameters and receive a typed result.",{"question":103,"answer":104},"Can users or the OS restrict which AppFunctions run?","Yes. AppFunctions follow Android's permission and user-consent model, and sensitive actions should require explicit confirmation or foreground verification before mutating user data.",{"id":20,"name":106,"slug":107,"description":108},"On-device AI","on-device-ai","Running models on the phone, not the cloud.",[110,113,114,118,122],{"id":30,"name":111,"slug":112,"description":53},"AI Development","ai-development",{"id":61,"name":62,"slug":63,"description":53},{"id":115,"name":116,"slug":117,"description":53},35,"Android 17","android-17",{"id":119,"name":120,"slug":121,"description":53},3,"Gemini Nano","gemini-nano",{"id":123,"name":106,"slug":107,"description":53},51,{"id":20,"name":4,"username":79,"avatar":53,"profile_path":80},"2026-11-28 10:11:52","2026-11-28 10:15:23",{"id":128,"title":129,"slug":130,"path":131,"excerpt":132,"cover_image":133,"cover_media_id":134,"status":40,"publish_at":135,"reading_time":27,"seo_title":136,"seo_description":137,"og_image":133,"canonical_url":138,"noindex":45,"featured":45,"faq":139,"howto":53,"review_note":53,"submitted_at":53,"reviewed_at":53,"category":146,"series":53,"tags":147,"author":156,"created_at":157,"updated_at":135},25,"Android 17's Mandatory Adaptive-First & Large-Screen Resizability Rules","android-17-adaptive-first-large-screen-resizability","\u002Fposts\u002Fandroid-17-adaptive-first-large-screen-resizability","Android 17 enforces adaptive-first layouts and large-screen resizability. Here is how to audit orientation locks, adopt WindowSizeClass, and ship ListDetailPaneScaffold.","\u002Fmedia\u002F2026\u002F11\u002F2df599bf25a8e4e3.webp",67,"2026-11-28 10:15:06","Android 17 Adaptive-First & Resizability Requirements","Prepare your Android app for Android 17's mandatory large-screen resizability and adaptive-first layout rules using WindowSizeClass and Material 3 adaptive scaffolds.","https:\u002F\u002Fwww.androidlearner.com\u002Fposts\u002Fandroid-17-adaptive-first-large-screen-resizability\u002F",[140,143],{"question":141,"answer":142},"Can I still lock screenOrientation=portrait on tablets and foldables in Android 17?","Android 17 removes legacy opt-outs for resizability on large screens (smallest width >= 600dp), meaning the system ignores fixed portrait\u002Flandscape locks and non-resizable flags.",{"question":144,"answer":145},"What is the fastest way to make an existing Compose screen adaptive?","Read currentWindowAdaptiveInfo() from Material 3 Adaptive, avoid hardcoded dp widths, and use ListDetailPaneScaffold or SupportingPaneScaffold for two-pane tablet and desktop layouts.",{"id":55,"name":56,"slug":57,"description":58},[148,149,150,151,152],{"id":61,"name":62,"slug":63,"description":53},{"id":115,"name":116,"slug":117,"description":53},{"id":65,"name":56,"slug":57,"description":53},{"id":71,"name":72,"slug":73,"description":53},{"id":153,"name":154,"slug":155,"description":53},46,"Material 3","material-3",{"id":20,"name":4,"username":79,"avatar":53,"profile_path":80},"2026-11-28 10:11:53",{"id":67,"title":159,"slug":160,"path":161,"excerpt":162,"cover_image":163,"cover_media_id":164,"status":40,"publish_at":165,"reading_time":27,"seo_title":166,"seo_description":167,"og_image":163,"canonical_url":168,"noindex":45,"featured":45,"faq":169,"howto":53,"review_note":53,"submitted_at":53,"reviewed_at":53,"category":176,"series":53,"tags":178,"author":189,"created_at":157,"updated_at":165},"Google's \"Compose First\" Era: Migrating Legacy XML & RecyclerView Screens","compose-first-migrating-legacy-xml-recyclerview","\u002Fposts\u002Fcompose-first-migrating-legacy-xml-recyclerview","With android.widget Views, Fragments, and XML layout editors officially in maintenance mode, here is a practical, incremental playbook for migrating to Jetpack Compose.","\u002Fmedia\u002F2026\u002F11\u002F5f47e539df8d96b9.webp",66,"2026-11-28 10:14:54","Compose First Migration Guide: XML & RecyclerView to Compose","Step-by-step guide to migrating legacy Android XML layouts, Fragments, and RecyclerView adapters to Jetpack Compose without rewriting your entire app at once.","https:\u002F\u002Fwww.androidlearner.com\u002Fposts\u002Fcompose-first-migrating-legacy-xml-recyclerview\u002F",[170,173],{"question":171,"answer":172},"Do I have to rewrite my entire app to adopt Compose First?","No. You can embed Compose inside existing XML layouts with ComposeView, replace RecyclerView item types incrementally, and migrate Fragment navigation screen by screen.",{"question":174,"answer":175},"Are XML Views being removed from Android?","No, existing View APIs remain in the Android SDK for compatibility, but they are in maintenance mode—meaning critical bug fixes only, while all new UI capabilities ship in Jetpack Compose.",{"id":27,"name":72,"slug":73,"description":177},"Modern Android UI.",[179,180,181,184,185],{"id":61,"name":62,"slug":63,"description":53},{"id":65,"name":56,"slug":57,"description":53},{"id":55,"name":182,"slug":183,"description":53},"Compose","compose",{"id":71,"name":72,"slug":73,"description":53},{"id":186,"name":187,"slug":188,"description":53},31,"Recycler View","recycler-view",{"id":20,"name":4,"username":79,"avatar":53,"profile_path":80},{"id":191,"title":192,"slug":193,"path":194,"excerpt":195,"cover_image":196,"cover_media_id":197,"status":40,"publish_at":198,"reading_time":199,"seo_title":200,"seo_description":201,"og_image":196,"canonical_url":202,"noindex":45,"featured":45,"faq":203,"howto":53,"review_note":53,"submitted_at":53,"reviewed_at":53,"category":210,"series":53,"tags":214,"author":226,"created_at":227,"updated_at":228},22,"Google Play's 16 KB Page Size Rule: What to Fix Before 1 February 2027","android-16kb-page-size-google-play-deadline","\u002Fposts\u002Fandroid-16kb-page-size-google-play-deadline","Apps that target API 35+ and ship native code must support 16 KB pages before 1 February 2027 or Play will block updates. Pure Kotlin and Java apps are already compatible.","\u002Fmedia\u002F2026\u002F11\u002F723c07ce07ee95c5.webp",62,"2026-11-25 02:00:00",8,"Android 16 KB page size: Play deadline 1 February 2027","If your app targets API 35+ and ships native code, Google Play will block updates after 1 February 2027 unless you support 16 KB pages. How to check, rebuild, and test.","https:\u002F\u002Fwww.androidlearner.com\u002Fposts\u002Fandroid-16kb-page-size-google-play-deadline\u002F",[204,207],{"question":205,"answer":206},"Does a pure Kotlin or Java app need a native rebuild?","No. Android Developers say that if the app and all of its libraries are written only in Java or Kotlin, it already supports 16 KB devices. They still recommend testing on a 16 KB emulator image.",{"question":208,"answer":209},"Will Google remove my app from Play on 1 February 2027 if I miss the deadline?","The official compatibility section says you will not be able to release updates that do not support 16 KB pages. It does not say existing listings are removed. Check Play Console for the wording on your own app, and re-read the page-sizes guide for the current date.",{"id":75,"name":211,"slug":212,"description":213},"Performance","performance","Startup, frames, and profiles.",[215,216,220,224],{"id":61,"name":62,"slug":63,"description":53},{"id":217,"name":218,"slug":219,"description":53},49,"Google Play","google-play",{"id":221,"name":222,"slug":223,"description":53},50,"NDK","ndk",{"id":225,"name":211,"slug":212,"description":53},12,{"id":20,"name":4,"username":79,"avatar":53,"profile_path":80},"2026-11-24 19:51:51","2026-11-24 19:53:15",[],"\u003Cp>Most Android developers know \u003Ccode>mutableStateOf\u003C\u002Fcode> and \u003Ccode>derivedStateOf\u003C\u002Fcode> as tools inside \u003Ccode>@Composable\u003C\u002Fcode> functions. Fewer realize that Compose's \u003Cstrong>snapshot state system\u003C\u002Fstrong> is a standalone, thread-safe transactional state engine—and the foundation behind \u003Cstrong>Jetpack AppState\u003C\u002Fstrong> patterns.\u003C\u002Fp>\n\u003Ch2 id=\"why-snapshot-state-works-outside-the-ui\">Why Snapshot State Works Outside the UI\u003C\u002Fh2>\n\u003Cp>Traditional \u003Ccode>StateFlow\u003C\u002Fcode> pipelines often require verbose \u003Ccode>combine(...)\u003C\u002Fcode> chains when multiple slices of state depend on each other. Snapshot state solves this with \u003Cstrong>automatic dependency tracking\u003C\u002Fstrong> and \u003Cstrong>atomic transactions\u003C\u002Fstrong>:\u003C\u002Fp>\n\u003Cul>\n\u003Cli>\u003Cstrong>Fine-grained reads\u003C\u002Fstrong>: Consumers only re-evaluate when the exact fields they read actually change.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Atomic updates\u003C\u002Fstrong>: \u003Ccode>Snapshot.withMutableSnapshot\u003C\u002Fcode> updates multiple fields together so observers never see a half-updated state.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Zero UI coupling\u003C\u002Fstrong>: \u003Ccode>androidx.compose.runtime\u003C\u002Fcode> works in pure Kotlin modules, \u003Ccode>ViewModel\u003C\u002Fcode>s, and services.\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Ch2 id=\"building-a-snapshot-backed-state-store\">Building a Snapshot-Backed State Store\u003C\u002Fh2>\n\u003Cp>You can encapsulate domain state in a plain Kotlin class backed by \u003Ccode>mutableStateOf\u003C\u002Fcode> and \u003Ccode>derivedStateOf\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\"> CartStateStore\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\"> items \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">by\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> mutableStateOf\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">&#x3C;\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">List\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">&#x3C;\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">CartItem\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">>>(\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">emptyList\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\">    var\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> promoDiscountPercent \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:#79B8FF\">0\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\">    val\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> totalCents: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">Int\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> by\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> derivedStateOf\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\"> subtotal \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> items.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">sumOf\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> { it.priceCents \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">*\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> it.quantity }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        subtotal \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">-\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> (subtotal \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">*\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> promoDiscountPercent \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">\u002F\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\"> 100\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\">    fun\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> applyBundle\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(newItems: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">List\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">&#x3C;\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">CartItem\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">>, discount: \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\">        Snapshot.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">withMutableSnapshot\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            items \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> newItems\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            promoDiscountPercent \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> discount\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\">\u003Cspan style=\"color:#E1E4E8\">}\u003C\u002Fspan>\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>Because \u003Ccode>applyBundle\u003C\u002Fcode> runs inside \u003Ccode>Snapshot.withMutableSnapshot\u003C\u002Fcode>, \u003Ccode>totalCents\u003C\u002Fcode> and any UI reading \u003Ccode>items\u003C\u002Fcode> + \u003Ccode>promoDiscountPercent\u003C\u002Fcode> observe both changes atomically.\u003C\u002Fp>\n\u003Ch2 id=\"bridging-snapshot-state-to-coroutines-widgets\">Bridging Snapshot State to Coroutines &amp; Widgets\u003C\u002Fh2>\n\u003Cp>When non-Compose consumers (like a \u003Ccode>WorkManager\u003C\u002Fcode> sync job, analytics logger, or Glance widget updater) need a cold stream of changes, use \u003Ccode>snapshotFlow\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\"> CartSyncObserver\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\"> val\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> store: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">CartStateStore\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\"> val\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> widgetUpdater: \u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">WidgetUpdater\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">    suspend\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> fun\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> observeAndSync\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">() {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#B392F0\">        snapshotFlow\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> { store.items.size to store.totalCents }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            .\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">distinctUntilChanged\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">()\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            .\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">collect\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> { (count, total) \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">->\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">                widgetUpdater.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">updateCartBadge\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(itemCount \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> count, totalCents \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> total)\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\">\u003Cspan style=\"color:#E1E4E8\">}\u003C\u002Fspan>\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Ch2 id=\"when-to-use-snapshot-state-vs-stateflow\">When to Use Snapshot State vs. \u003Ccode>StateFlow\u003C\u002Fcode>\u003C\u002Fh2>\n\u003Cul>\n\u003Cli>\u003Cstrong>Use \u003Ccode>StateFlow\u003C\u002Fcode> \u002F \u003Ccode>Flow\u003C\u002Fcode>\u003C\u002Fstrong> for asynchronous I\u002FO streams (Room queries, WebSockets, Ktor responses).\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Use Snapshot State \u002F AppState\u003C\u002Fstrong> for synchronous, interconnected UI and session state where derived values and atomic multi-field updates matter.\u003C\u002Fli>\n\u003C\u002Ful>\n",1795877411955]