{"id":160,"date":"2026-10-18T10:45:00","date_gmt":"2026-10-18T10:45:00","guid":{"rendered":"https:\/\/moiks.com\/blog\/?p=160"},"modified":"2026-11-06T08:48:31","modified_gmt":"2026-11-06T08:48:31","slug":"kotlin-stateflow-in-android-complete-guide-with-viewmodel-jetpack-compose","status":"publish","type":"post","link":"https:\/\/moiks.com\/blog\/kotlin-stateflow-in-android-complete-guide-with-viewmodel-jetpack-compose\/","title":{"rendered":"Kotlin StateFlow in Android: Complete Guide with ViewModel &#038; Jetpack Compose"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Managing UI state is one of the most important responsibilities in modern Android development.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Every Android application needs to handle changing information, such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Loading data from an API<\/li>\n\n\n\n<li>Updating a shopping cart<\/li>\n\n\n\n<li>Managing user preferences<\/li>\n\n\n\n<li>Displaying database records<\/li>\n\n\n\n<li>Updating a login form<\/li>\n\n\n\n<li>Tracking download progress<\/li>\n\n\n\n<li>Handling loading, success, and error states<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In modern Android development, <strong>Kotlin StateFlow<\/strong> provides a powerful way to manage and observe these changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow works especially well with <strong>ViewModel, Kotlin Coroutines, MVVM architecture, and Jetpack Compose<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we&#8217;ll explore Kotlin StateFlow step by step and build a practical Android application using Jetpack Compose.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What You&#8217;ll Learn<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this tutorial, you&#8217;ll understand:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What Kotlin StateFlow is<\/li>\n\n\n\n<li>Why Android developers use StateFlow<\/li>\n\n\n\n<li>MutableStateFlow vs StateFlow<\/li>\n\n\n\n<li>How StateFlow works<\/li>\n\n\n\n<li>StateFlow vs regular Flow<\/li>\n\n\n\n<li>StateFlow vs LiveData<\/li>\n\n\n\n<li>StateFlow vs SharedFlow<\/li>\n\n\n\n<li>StateFlow and equality-based conflation<\/li>\n\n\n\n<li>Updating state safely<\/li>\n\n\n\n<li>Immutable UI state<\/li>\n\n\n\n<li>StateFlow with ViewModel<\/li>\n\n\n\n<li>StateFlow with Jetpack Compose<\/li>\n\n\n\n<li>collectAsStateWithLifecycle()<\/li>\n\n\n\n<li>stateIn()<\/li>\n\n\n\n<li>SharingStarted<\/li>\n\n\n\n<li>Combining multiple StateFlows<\/li>\n\n\n\n<li>Loading, success, and error states<\/li>\n\n\n\n<li>Practical Android mini-project<\/li>\n\n\n\n<li>Common mistakes and best practices<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s begin!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. What Is Kotlin StateFlow?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is a hot Flow that represents observable state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It always contains a current value and emits updates to collectors when its value changes according to equality.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Simple Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>val counter = MutableStateFlow(0)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the initial value is 0.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can update it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>counter.value = 10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And access its current value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>println(counter.value)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Key Characteristics<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Always has an initial value<\/li>\n\n\n\n<li>Stores the current state<\/li>\n\n\n\n<li>Supports multiple collectors<\/li>\n\n\n\n<li>Replays the latest value to new collectors<\/li>\n\n\n\n<li>Uses equality-based conflation<\/li>\n\n\n\n<li>Remains active independently of collectors<\/li>\n\n\n\n<li>Does not complete normally<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Why Is StateFlow Useful?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Imagine a shopping cart application.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the user adds an item, the cart total should update automatically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of manually refreshing the UI, StateFlow allows the ViewModel to publish the latest state while Jetpack Compose observes it.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Why Use StateFlow in Android?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Traditional Android applications often relied on callbacks, listeners, or manually updated views.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Modern Android applications benefit from reactive state management.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Traditional Approach<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A screen requests data and manually updates its views when a callback returns.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This can become difficult to manage as the application grows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">StateFlow Approach<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A ViewModel exposes observable state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI observes that state and updates automatically.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Typical Architecture<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Repository\n    |\n    v\nViewModel\n    |\n    v\nMutableStateFlow\n    |\n    v\nStateFlow\n    |\n    v\nJetpack Compose UI<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clear separation of responsibilities<\/li>\n\n\n\n<li>Lifecycle-aware UI collection<\/li>\n\n\n\n<li>Predictable state updates<\/li>\n\n\n\n<li>Better testability<\/li>\n\n\n\n<li>Easier integration with Kotlin Coroutines<\/li>\n\n\n\n<li>Cleaner MVVM architecture<\/li>\n\n\n\n<li>Reduced manual UI synchronization<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Add StateFlow Dependencies<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is part of Kotlin Coroutines.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For Android applications, include the coroutine and lifecycle dependencies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">build.gradle.kts<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>dependencies {\n\n    implementation(\n        \"org.jetbrains.kotlinx:kotlinx-coroutines-android:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.lifecycle:lifecycle-viewmodel-ktx:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.lifecycle:lifecycle-runtime-compose:&lt;version&gt;\"\n    )\n\n    implementation(\n        \"androidx.lifecycle:lifecycle-viewmodel-compose:&lt;version&gt;\"\n    )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace each placeholder with a compatible stable version.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you use a Gradle Version Catalog, declare these dependencies in <code>libs.versions.toml<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Imports<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import kotlinx.coroutines.flow.MutableStateFlow\nimport kotlinx.coroutines.flow.StateFlow\nimport kotlinx.coroutines.flow.asStateFlow\nimport kotlinx.coroutines.flow.update<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. MutableStateFlow vs StateFlow<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin provides two closely related interfaces.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">MutableStateFlow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MutableStateFlow allows reading and updating the current state.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val count = MutableStateFlow(0)\n\ncount.value = 5<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">StateFlow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow exposes a read-only view of the state.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val count: StateFlow&lt;Int&gt; =\n    MutableStateFlow(0)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The public StateFlow interface does not expose a value setter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Recommended Android Pattern<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class CounterViewModel : ViewModel() {\n\n    private val _count = MutableStateFlow(0)\n\n    val count: StateFlow&lt;Int&gt; =\n        _count.asStateFlow()\n\n    fun increment() {\n        _count.update { current -&gt;\n            current + 1\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel owns the mutable state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI observes the public StateFlow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Only the ViewModel can change the state through its exposed methods.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Keep MutableStateFlow private and expose StateFlow publicly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This follows the principle of encapsulation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. How StateFlow Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s understand how StateFlow communicates updates.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    val counter = MutableStateFlow(0)\n\n    val job = launch {\n\n        counter.collect { value -&gt;\n            println(\"Counter: $value\")\n        }\n    }\n\n    job.start()\n\n    counter.subscriptionCount.first {\n        it &gt; 0\n    }\n\n    counter.value = 10\n\n    delay(100)\n\n    counter.value = 20\n\n    delay(100)\n\n    job.cancelAndJoin()\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The StateFlow stores the latest value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Collectors observe changes to that value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">New collectors immediately receive the current state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is conflated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A slow collector may skip intermediate updates and receive only the most recent state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Therefore, StateFlow is designed for state observation, not guaranteed delivery of every intermediate event.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. StateFlow vs Regular Flow<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both StateFlow and Flow belong to the Kotlin Flow API, but they have different behavior.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Feature<\/th><th>Flow<\/th><th>StateFlow<\/th><\/tr><tr><td>Cold or hot<\/td><td>Depends on implementation<\/td><td>Hot<\/td><\/tr><tr><td>Initial value required<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Current value property<\/td><td>Not generally<\/td><td>Yes<\/td><\/tr><tr><td>Multiple collectors<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td>Replays current state<\/td><td>Not generally<\/td><td>Yes<\/td><\/tr><tr><td>Equality-based conflation<\/td><td>Not generally<\/td><td>Yes<\/td><\/tr><tr><td>Main use<\/td><td>Asynchronous streams<\/td><td>Observable state<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">When to Use Flow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use regular Flow for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Database queries<\/li>\n\n\n\n<li>Asynchronous data pipelines<\/li>\n\n\n\n<li>Network-driven streams<\/li>\n\n\n\n<li>Data transformations<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">When to Use StateFlow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use StateFlow for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Screen state<\/li>\n\n\n\n<li>Selected items<\/li>\n\n\n\n<li>Form input<\/li>\n\n\n\n<li>Loading status<\/li>\n\n\n\n<li>Dashboard information<\/li>\n\n\n\n<li>User settings<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. StateFlow vs LiveData<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">LiveData is a lifecycle-aware observable data holder widely used in Android development.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is a coroutine-based alternative commonly used in modern Android applications.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Feature<\/th><th>StateFlow<\/th><th>LiveData<\/th><\/tr><tr><td>Kotlin Coroutines integration<\/td><td>Native<\/td><td>Via extensions<\/td><\/tr><tr><td>Initial value<\/td><td>Required<\/td><td>Optional<\/td><\/tr><tr><td>Current value<\/td><td>Available<\/td><td>Available, may be null<\/td><\/tr><tr><td>Flow operators<\/td><td>Supported<\/td><td>Requires conversion<\/td><\/tr><tr><td>Lifecycle awareness<\/td><td>Through collection APIs<\/td><td>Built into observation<\/td><\/tr><tr><td>Jetpack Compose support<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td>Recommended modern use<\/td><td>Common for coroutine-based architectures<\/td><td>Supported, especially in existing apps<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Important Difference<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow does not automatically stop collecting based on Android lifecycle state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use lifecycle-aware APIs such as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>collectAsStateWithLifecycle()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For Activity and Fragment collection, use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>repeatOnLifecycle()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Which Should You Choose?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For new Android applications built using Kotlin Coroutines, ViewModel, and Jetpack Compose, StateFlow is an excellent default for UI state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">LiveData remains valid for existing projects.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. StateFlow vs SharedFlow<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both StateFlow and SharedFlow are hot Flows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, they are designed for different purposes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">StateFlow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Represents current observable state.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val uiState = MutableStateFlow(\"Loading\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">SharedFlow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Broadcasts values to subscribers with configurable replay and buffering.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>val events = MutableSharedFlow&lt;String&gt;()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Comparison<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Feature<\/th><th>StateFlow<\/th><th>SharedFlow<\/th><\/tr><tr><td>Requires initial value<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Current value property<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Replay<\/td><td>Latest state<\/td><td>Configurable<\/td><\/tr><tr><td>Equality conflation<\/td><td>Yes<\/td><td>Not automatic<\/td><\/tr><tr><td>Typical use<\/td><td>UI state<\/td><td>Broadcast events<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use StateFlow to represent whether a form is valid.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use SharedFlow when broadcasting updates to multiple active subscribers is appropriate.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Do not assume SharedFlow guarantees one-time event delivery.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Without suitable replay or buffering, emissions can be missed when no subscriber is active.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9. Updating StateFlow Values<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow provides multiple ways to update state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Direct Assignment<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>_count.value = 10<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Updating Based on Previous State<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>_count.update { current -&gt;\n    current + 1\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Why Prefer update()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The update function performs an atomic update based on the current value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is useful when multiple coroutines may update the same state concurrently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class CounterViewModel : ViewModel() {\n\n    private val _count = MutableStateFlow(0)\n\n    val count = _count.asStateFlow()\n\n    fun increment() {\n\n        _count.update { current -&gt;\n            current + 1\n        }\n    }\n\n    fun decrement() {\n\n        _count.update { current -&gt;\n            current - 1\n        }\n    }\n\n    fun reset() {\n\n        _count.value = 0\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">10. StateFlow and Equality-Based Conflation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow uses equality to determine whether a new value represents a change.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>val name = MutableStateFlow(\"Android\")\n\nname.value = \"Android\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Because the new value is equal to the current value, StateFlow does not emit a new update for this assignment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>name.value = \"Jetpack Compose\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This value differs from the previous state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Active collectors can observe the updated value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is not intended for repeated identical events.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, showing the same Snackbar message twice should not rely on assigning the same string to a StateFlow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For UI events, consider explicit event state, an event queue, or another suitable event mechanism.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">11. Immutable UI State with Data Classes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Modern Android applications commonly represent screen state using immutable data classes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class ProfileUiState(\n    val name: String = \"\",\n    val email: String = \"\",\n    val isLoading: Boolean = false,\n    val errorMessage: String? = null\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">ViewModel<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class ProfileViewModel : ViewModel() {\n\n    private val _uiState = MutableStateFlow(\n        ProfileUiState()\n    )\n\n    val uiState = _uiState.asStateFlow()\n\n    fun updateName(name: String) {\n\n        _uiState.update { current -&gt;\n            current.copy(name = name)\n        }\n    }\n\n    fun updateEmail(email: String) {\n\n        _uiState.update { current -&gt;\n            current.copy(email = email)\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of modifying individual properties of a mutable object, we create a new state value using copy().<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This approach makes state changes easier to understand and test.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer immutable state models containing immutable collections and values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid modifying mutable lists inside a StateFlow without publishing a new state.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">12. StateFlow with ViewModel<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">ViewModel is responsible for holding and managing UI-related state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow works naturally inside ViewModel.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class GreetingViewModel : ViewModel() {\n\n    private val _message = MutableStateFlow(\n        \"Welcome to Android\"\n    )\n\n    val message = _message.asStateFlow()\n\n    fun updateMessage() {\n\n        _message.value =\n            \"Welcome to Jetpack Compose\"\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel owns the message state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI observes it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When updateMessage() is called, the UI can display the updated value.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">13. StateFlow with Jetpack Compose<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Jetpack Compose can collect StateFlow and automatically update the UI.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun GreetingScreen(\n    viewModel: GreetingViewModel = viewModel()\n) {\n\n    val message by viewModel.message\n        .collectAsStateWithLifecycle()\n\n    Column(\n        modifier = Modifier\n            .fillMaxSize()\n            .padding(24.dp),\n        verticalArrangement = Arrangement.Center\n    ) {\n\n        Text(\n            text = message,\n            style = MaterialTheme.typography.headlineMedium\n        )\n\n        Spacer(\n            modifier = Modifier.height(16.dp)\n        )\n\n        Button(\n            onClick = {\n                viewModel.updateMessage()\n            }\n        ) {\n\n            Text(\"Update Message\")\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">collectAsStateWithLifecycle() converts the StateFlow into Compose State while respecting the Android lifecycle.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the StateFlow value changes, Compose can recompose the affected UI.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">14. What Is collectAsStateWithLifecycle()?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The collectAsStateWithLifecycle() function is provided by Android Lifecycle Compose integration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It collects a Flow or StateFlow while the lifecycle is in an appropriate active state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>val uiState by viewModel.uiState\n    .collectAsStateWithLifecycle()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Lifecycle-aware collection<\/li>\n\n\n\n<li>Integration with Compose State<\/li>\n\n\n\n<li>Reduced unnecessary background collection<\/li>\n\n\n\n<li>Automatic UI updates<\/li>\n\n\n\n<li>Cleaner ViewModel integration<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The default minimum active lifecycle state is STARTED.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Collection is stopped when the lifecycle falls below the configured state and resumes when it becomes active again.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For Android Compose screens, prefer collectAsStateWithLifecycle() over collectAsState() when lifecycle awareness is required.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">15. Understanding stateIn()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The stateIn() operator converts a Flow into a StateFlow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is useful when a repository exposes a Flow but the ViewModel needs a current observable UI state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>val uiState: StateFlow&lt;List&lt;String&gt;&gt; =\n    repository.observeItems()\n        .stateIn(\n            scope = viewModelScope,\n            started = SharingStarted.WhileSubscribed(5000),\n            initialValue = emptyList()\n        )<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The upstream Flow is converted into a StateFlow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The StateFlow exposes a current value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Multiple collectors can observe the shared state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parameters<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>scope<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Defines the coroutine scope that manages sharing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>started<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Controls when the upstream Flow starts and stops collecting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>initialValue<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Defines the initial state before upstream data arrives.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">16. Understanding SharingStarted<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SharingStarted controls how a shared Flow starts and stops upstream collection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Common strategies include:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SharingStarted.Eagerly<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Starts collecting upstream immediately.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SharingStarted.Eagerly<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">SharingStarted.Lazily<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Starts collecting when the first subscriber appears and continues while the sharing scope remains active.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SharingStarted.Lazily<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">SharingStarted.WhileSubscribed()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Starts upstream collection when subscribers are present.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It can stop upstream collection after subscribers disappear.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SharingStarted.WhileSubscribed(\n    stopTimeoutMillis = 5000\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Why Use 5000 Milliseconds?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A five-second timeout is a common Android example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It allows upstream collection to remain active briefly during temporary UI interruptions, such as configuration changes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The timeout is not a universal requirement.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Choose an appropriate strategy based on the data source and application behavior.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">17. Combining Multiple StateFlows<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes a screen depends on multiple pieces of state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Product price<\/li>\n\n\n\n<li>Product quantity<\/li>\n\n\n\n<li>Discount percentage<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">We can combine these values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class CartViewModel : ViewModel() {\n\n    private val price = MutableStateFlow(100.0)\n\n    private val quantity = MutableStateFlow(1)\n\n    val totalPrice: StateFlow&lt;Double&gt; =\n        combine(\n            price,\n            quantity\n        ) { currentPrice, currentQuantity -&gt;\n\n            currentPrice * currentQuantity\n\n        }.stateIn(\n            scope = viewModelScope,\n            started = SharingStarted.WhileSubscribed(5000),\n            initialValue = 100.0\n        )\n\n    fun updateQuantity(value: Int) {\n\n        quantity.value = value.coerceAtLeast(1)\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Whenever price or quantity changes, combine calculates the latest total.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The result is exposed as StateFlow.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Uses<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Shopping carts<\/li>\n\n\n\n<li>Form validation<\/li>\n\n\n\n<li>Dashboard calculations<\/li>\n\n\n\n<li>User preferences<\/li>\n\n\n\n<li>Filtered lists<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">18. Representing Loading, Success, and Error States<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A common Android pattern is to represent the complete screen state explicitly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sealed Interface Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sealed interface UiState&lt;out T&gt; {\n\n    data object Loading : UiState&lt;Nothing&gt;\n\n    data class Success&lt;T&gt;(\n        val data: T\n    ) : UiState&lt;T&gt;\n\n    data class Error(\n        val message: String\n    ) : UiState&lt;Nothing&gt;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This approach represents mutually exclusive states.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI can display a loading indicator, content, or an error message.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clear state handling<\/li>\n\n\n\n<li>Exhaustive when expressions<\/li>\n\n\n\n<li>Easier testing<\/li>\n\n\n\n<li>Reduced invalid state combinations<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Practical Android Mini-Project: User Profile with StateFlow<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s build a simple user profile application using:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Kotlin<\/li>\n\n\n\n<li>Jetpack Compose<\/li>\n\n\n\n<li>Material 3<\/li>\n\n\n\n<li>ViewModel<\/li>\n\n\n\n<li>StateFlow<\/li>\n\n\n\n<li>Kotlin Coroutines<\/li>\n\n\n\n<li>MVVM architecture<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Our application will:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Load a user profile.<\/li>\n\n\n\n<li>Display a loading indicator.<\/li>\n\n\n\n<li>Show the user&#8217;s name and email.<\/li>\n\n\n\n<li>Allow refreshing the profile.<\/li>\n\n\n\n<li>Display an error when loading fails.<\/li>\n\n\n\n<li>Update the UI automatically.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">19. Create the User Model<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>data class User(\n    val id: Int,\n    val name: String,\n    val email: String\n)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This model represents a user profile.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">20. Create the User Repository<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For this tutorial, we&#8217;ll simulate a network request.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserRepository {\n\n    suspend fun getUser(): User {\n\n        delay(1200)\n\n        return User(\n            id = 101,\n            name = \"Alex Johnson\",\n            email = \"alex@example.com\"\n        )\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The repository uses delay() to simulate network latency.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a real application, this method could call a REST API using Retrofit.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">21. Create the Profile UI State<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>sealed interface ProfileUiState {\n\n    data object Loading : ProfileUiState\n\n    data class Success(\n        val user: User\n    ) : ProfileUiState\n\n    data class Error(\n        val message: String\n    ) : ProfileUiState\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The screen can be in one of three states:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Loading<\/li>\n\n\n\n<li>Success<\/li>\n\n\n\n<li>Error<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This keeps UI rendering predictable.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">22. Create the Profile ViewModel<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class ProfileViewModel : ViewModel() {\n\n    private val repository = UserRepository()\n\n    private val _uiState =\n        MutableStateFlow&lt;ProfileUiState&gt;(\n            ProfileUiState.Loading\n        )\n\n    val uiState: StateFlow&lt;ProfileUiState&gt; =\n        _uiState.asStateFlow()\n\n    private var loadJob: Job? = null\n\n    init {\n        loadProfile()\n    }\n\n    fun loadProfile() {\n\n        loadJob?.cancel()\n\n        loadJob = viewModelScope.launch {\n\n            _uiState.value = ProfileUiState.Loading\n\n            try {\n\n                val user = repository.getUser()\n\n                _uiState.value =\n                    ProfileUiState.Success(user)\n\n            } catch (e: CancellationException) {\n\n                throw e\n\n            } catch (e: Exception) {\n\n                _uiState.value =\n                    ProfileUiState.Error(\n                        \"Unable to load profile\"\n                    )\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel creates a StateFlow that represents the screen state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When loading begins, it emits Loading.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the repository returns a user, it emits Success.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If an ordinary exception occurs, it emits Error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why Track loadJob?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The example cancels a previous loading operation before starting another.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This prevents an older request from overwriting a newer result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">CancellationException is rethrown to preserve coroutine cancellation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For production applications, consider constructor-injecting the repository with Hilt instead of creating it directly inside the ViewModel.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">23. Create the Jetpack Compose Profile Screen<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s build the UI.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun ProfileScreen(\n    viewModel: ProfileViewModel = viewModel()\n) {\n\n    val uiState by viewModel.uiState\n        .collectAsStateWithLifecycle()\n\n    Column(\n        modifier = Modifier\n            .fillMaxSize()\n            .padding(24.dp),\n        verticalArrangement = Arrangement.Center,\n        horizontalAlignment = Alignment.CenterHorizontally\n    ) {\n\n        Text(\n            text = \"User Profile\",\n            style = MaterialTheme.typography.headlineMedium\n        )\n\n        Spacer(\n            modifier = Modifier.height(24.dp)\n        )\n\n        when (val state = uiState) {\n\n            ProfileUiState.Loading -&gt; {\n\n                CircularProgressIndicator()\n\n                Spacer(\n                    modifier = Modifier.height(16.dp)\n                )\n\n                Text(\"Loading profile...\")\n            }\n\n            is ProfileUiState.Success -&gt; {\n\n                Card(\n                    modifier = Modifier.fillMaxWidth()\n                ) {\n\n                    Column(\n                        modifier = Modifier.padding(20.dp),\n                        verticalArrangement =\n                            Arrangement.spacedBy(12.dp)\n                    ) {\n\n                        Text(\n                            text = state.user.name,\n                            style = MaterialTheme.typography.titleLarge\n                        )\n\n                        Text(\n                            text = state.user.email,\n                            style = MaterialTheme.typography.bodyMedium\n                        )\n\n                        Text(\n                            text = \"User ID: ${state.user.id}\"\n                        )\n                    }\n                }\n\n                Spacer(\n                    modifier = Modifier.height(20.dp)\n                )\n\n                Button(\n                    onClick = {\n                        viewModel.loadProfile()\n                    }\n                ) {\n\n                    Text(\"Refresh Profile\")\n                }\n            }\n\n            is ProfileUiState.Error -&gt; {\n\n                Text(\n                    text = state.message,\n                    color = MaterialTheme.colorScheme.error\n                )\n\n                Spacer(\n                    modifier = Modifier.height(16.dp)\n                )\n\n                Button(\n                    onClick = {\n                        viewModel.loadProfile()\n                    }\n                ) {\n\n                    Text(\"Retry\")\n                }\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Compose screen observes the ViewModel&#8217;s StateFlow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the state changes, Compose updates the screen.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The UI does not need to manually manage callbacks.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">24. Required Imports<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Coroutines and Flow<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import kotlinx.coroutines.CancellationException\nimport kotlinx.coroutines.Job\nimport kotlinx.coroutines.delay\nimport kotlinx.coroutines.launch\nimport kotlinx.coroutines.flow.MutableStateFlow\nimport kotlinx.coroutines.flow.StateFlow\nimport kotlinx.coroutines.flow.asStateFlow\nimport kotlinx.coroutines.flow.update<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">ViewModel and Lifecycle<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import androidx.lifecycle.ViewModel\nimport androidx.lifecycle.viewModelScope\nimport androidx.lifecycle.viewmodel.compose.viewModel\nimport androidx.lifecycle.compose.collectAsStateWithLifecycle<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Jetpack Compose<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import androidx.compose.foundation.layout.Arrangement\nimport androidx.compose.foundation.layout.Column\nimport androidx.compose.foundation.layout.Spacer\nimport androidx.compose.foundation.layout.fillMaxSize\nimport androidx.compose.foundation.layout.fillMaxWidth\nimport androidx.compose.foundation.layout.height\nimport androidx.compose.foundation.layout.padding\nimport androidx.compose.material3.Button\nimport androidx.compose.material3.Card\nimport androidx.compose.material3.CircularProgressIndicator\nimport androidx.compose.material3.MaterialTheme\nimport androidx.compose.material3.Text\nimport androidx.compose.runtime.Composable\nimport androidx.compose.runtime.getValue\nimport androidx.compose.ui.Alignment\nimport androidx.compose.ui.Modifier\nimport androidx.compose.ui.unit.dp<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">25. Expected UI \u2014 Loading<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|                                |\n|          User Profile          |\n|                                |\n|              \u25cb                 |\n|                                |\n|       Loading profile...       |\n|                                |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">26. Expected UI \u2014 Success<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|                                |\n|          User Profile          |\n|                                |\n| +----------------------------+ |\n| |                            | |\n| | Alex Johnson               | |\n| |                            | |\n| | alex@example.com           | |\n| |                            | |\n| | User ID: 101               | |\n| |                            | |\n| +----------------------------+ |\n|                                |\n|       &#91; Refresh Profile ]      |\n|                                |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">27. Expected UI \u2014 Error<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|                                |\n|          User Profile          |\n|                                |\n|     Unable to load profile     |\n|                                |\n|            &#91; Retry ]           |\n|                                |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">28. How the Complete Application Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s understand the complete data flow.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: ViewModel Initialization<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel is created when the screen first requests it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Repository Request<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel calls the repository&#8217;s suspend function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Loading State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The StateFlow contains ProfileUiState.Loading.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Repository Result<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The repository returns the user information.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: StateFlow Update<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel updates the StateFlow with ProfileUiState.Success.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Compose Observation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">collectAsStateWithLifecycle() observes the latest state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 7: UI Recomposition<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Jetpack Compose updates the displayed content.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 8: Refresh<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When the user taps Refresh Profile, the ViewModel starts another request.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This demonstrates a simple reactive Android architecture.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">29. StateFlow with Room Database<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is also useful when working with Room.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose a Room DAO exposes a Flow of users.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">DAO<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Dao\ninterface UserDao {\n\n    @Query(\"SELECT * FROM users\")\n    fun observeUsers(): Flow&lt;List&lt;UserEntity&gt;&gt;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Repository<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserRepository(\n    private val userDao: UserDao\n) {\n\n    fun observeUsers(): Flow&lt;List&lt;UserEntity&gt;&gt; {\n        return userDao.observeUsers()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">ViewModel<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class UsersViewModel(\n    repository: UserRepository\n) : ViewModel() {\n\n    val users: StateFlow&lt;List&lt;UserEntity&gt;&gt; =\n        repository.observeUsers()\n            .stateIn(\n                scope = viewModelScope,\n                started = SharingStarted.WhileSubscribed(5000),\n                initialValue = emptyList()\n            )\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Room emits query results when relevant database changes occur.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel converts the repository Flow into StateFlow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Jetpack Compose can observe the resulting state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This pattern is useful for offline-first Android applications.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">30. Common StateFlow Mistakes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 1: Exposing MutableStateFlow Publicly<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid allowing external components to modify ViewModel state directly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Expose StateFlow instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 2: Mutating Existing State Objects<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid changing mutable properties inside the existing state without publishing a new value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer immutable models and copy().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 3: Using StateFlow for Repeated Identical Events<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow suppresses equal consecutive values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use an appropriate event mechanism when every event must be handled.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 4: Ignoring Lifecycle-Aware Collection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use collectAsStateWithLifecycle() in Android Compose screens.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 5: Starting Unmanaged Coroutines<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use viewModelScope for ViewModel-owned operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 6: Ignoring Cancellation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Preserve CancellationException when handling broad exceptions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 7: Creating Multiple Sources of Truth<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid maintaining duplicate mutable copies of the same UI state in different layers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 8: Assuming Every Intermediate State Is Delivered<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is conflated.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Slow collectors can skip intermediate values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 9: Using Incorrect SharingStarted Behavior<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Choose Eagerly, Lazily, or WhileSubscribed based on actual lifecycle and data requirements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 10: Performing Business Logic Inside Composables<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Keep business logic in ViewModels and repositories.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Composables should primarily display state and forward user actions.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">31. Kotlin StateFlow Best Practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Follow these recommendations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use StateFlow for observable UI state.<\/li>\n\n\n\n<li>Keep MutableStateFlow private.<\/li>\n\n\n\n<li>Expose read-only StateFlow.<\/li>\n\n\n\n<li>Prefer immutable data classes.<\/li>\n\n\n\n<li>Use update() for atomic state transformations.<\/li>\n\n\n\n<li>Use ViewModel for screen state ownership.<\/li>\n\n\n\n<li>Use viewModelScope for asynchronous screen operations.<\/li>\n\n\n\n<li>Use collectAsStateWithLifecycle() in Compose.<\/li>\n\n\n\n<li>Represent loading, success, and error states explicitly.<\/li>\n\n\n\n<li>Use stateIn() when converting repository Flows into StateFlow.<\/li>\n\n\n\n<li>Choose SharingStarted intentionally.<\/li>\n\n\n\n<li>Avoid duplicate sources of truth.<\/li>\n\n\n\n<li>Preserve coroutine cancellation.<\/li>\n\n\n\n<li>Use Hilt for dependency injection in larger applications.<\/li>\n\n\n\n<li>Test state transitions.<\/li>\n\n\n\n<li>Avoid unnecessary recomposition by designing stable UI models.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">These practices help create maintainable and scalable Android applications.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">32. Kotlin StateFlow Quick Reference<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Concept<\/th><th>Purpose<\/th><\/tr><tr><td>StateFlow<\/td><td>Read-only observable state<\/td><\/tr><tr><td>MutableStateFlow<\/td><td>Mutable observable state<\/td><\/tr><tr><td>value<\/td><td>Access current state<\/td><\/tr><tr><td>asStateFlow()<\/td><td>Expose read-only interface<\/td><\/tr><tr><td>update()<\/td><td>Atomically update state<\/td><\/tr><tr><td>collect()<\/td><td>Collect emitted values<\/td><\/tr><tr><td>collectAsStateWithLifecycle()<\/td><td>Lifecycle-aware Compose collection<\/td><\/tr><tr><td>stateIn()<\/td><td>Convert Flow to StateFlow<\/td><\/tr><tr><td>SharingStarted.Eagerly<\/td><td>Start upstream immediately<\/td><\/tr><tr><td>SharingStarted.Lazily<\/td><td>Start on first subscriber<\/td><\/tr><tr><td>SharingStarted.WhileSubscribed()<\/td><td>Collect upstream while subscribed<\/td><\/tr><tr><td>combine()<\/td><td>Combine latest values<\/td><\/tr><tr><td>viewModelScope<\/td><td>ViewModel-owned coroutine scope<\/td><\/tr><tr><td>copy()<\/td><td>Create updated immutable state<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">33. Practical Exercises<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Try these exercises to strengthen your understanding.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 1: Basic StateFlow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a MutableStateFlow containing an integer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Update the value and observe it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 2: Counter ViewModel<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a ViewModel with increment, decrement, and reset functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 3: Immutable UI State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a data class containing name, email, and loading status.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Update it using copy().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 4: StateFlow and Compose<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Build a Compose screen that displays a StateFlow counter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 5: Loading State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a ViewModel that simulates loading data using delay().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 6: Error State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add an error state to your ViewModel.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 7: stateIn<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Convert a repository Flow into StateFlow.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 8: SharingStarted<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Experiment with Eagerly, Lazily, and WhileSubscribed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 9: combine<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Combine two StateFlows to calculate a shopping cart total.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 10: Profile Application<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Build the complete user profile mini-project from this tutorial.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">34. Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is StateFlow in Kotlin?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is a hot Flow that holds a current value and allows collectors to observe state changes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is MutableStateFlow?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MutableStateFlow is a mutable implementation of StateFlow that allows updating the current state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between StateFlow and MutableStateFlow?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow exposes state for reading and collection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MutableStateFlow also allows state updates.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is StateFlow lifecycle-aware?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow itself is not tied to Android lifecycle state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use lifecycle-aware collection APIs such as collectAsStateWithLifecycle().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between StateFlow and LiveData?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is part of Kotlin Coroutines and integrates with Flow operators.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">LiveData is an Android lifecycle-aware observable data holder.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is stateIn()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">stateIn converts a Flow into a StateFlow within a specified CoroutineScope.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is SharingStarted.WhileSubscribed()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It controls upstream collection based on the presence of subscribers, with configurable stopping behavior.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can StateFlow be used with Jetpack Compose?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. StateFlow works naturally with Jetpack Compose through collectAsStateWithLifecycle().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does StateFlow emit duplicate values?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow suppresses consecutive values that are equal according to equals().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can StateFlow replace LiveData?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, StateFlow is commonly used instead of LiveData in modern coroutine-based Android applications.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What We&#8217;ve Learned<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we learned:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Kotlin StateFlow fundamentals<\/li>\n\n\n\n<li>MutableStateFlow vs StateFlow<\/li>\n\n\n\n<li>StateFlow vs Flow<\/li>\n\n\n\n<li>StateFlow vs LiveData<\/li>\n\n\n\n<li>StateFlow vs SharedFlow<\/li>\n\n\n\n<li>Equality-based conflation<\/li>\n\n\n\n<li>Atomic state updates<\/li>\n\n\n\n<li>Immutable UI state<\/li>\n\n\n\n<li>ViewModel integration<\/li>\n\n\n\n<li>Jetpack Compose integration<\/li>\n\n\n\n<li>Lifecycle-aware collection<\/li>\n\n\n\n<li>stateIn<\/li>\n\n\n\n<li>SharingStarted<\/li>\n\n\n\n<li>Combining StateFlows<\/li>\n\n\n\n<li>Loading, success, and error states<\/li>\n\n\n\n<li>Practical user profile application<\/li>\n\n\n\n<li>Room database integration<\/li>\n\n\n\n<li>Common mistakes and best practices<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin StateFlow is one of the most important tools for managing UI state in modern Android development.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By combining StateFlow with ViewModel, Kotlin Coroutines, and Jetpack Compose, developers can create predictable, responsive, and maintainable Android applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding StateFlow also prepares you for advanced topics such as Clean Architecture, Room Database, Hilt dependency injection, and production-ready UI state management.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In our next tutorial, we&#8217;ll explore:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Kotlin SharedFlow in Android: Events, Replay, Buffering &amp; Jetpack Compose<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SharedFlow fundamentals<\/li>\n\n\n\n<li>MutableSharedFlow<\/li>\n\n\n\n<li>Hot streams<\/li>\n\n\n\n<li>Replay cache<\/li>\n\n\n\n<li>Buffering strategies<\/li>\n\n\n\n<li>emit vs tryEmit<\/li>\n\n\n\n<li>Multiple collectors<\/li>\n\n\n\n<li>SharedFlow vs StateFlow<\/li>\n\n\n\n<li>SharedFlow with ViewModel<\/li>\n\n\n\n<li>SharedFlow with Jetpack Compose<\/li>\n\n\n\n<li>UI events and Snackbar handling<\/li>\n\n\n\n<li>Common event delivery mistakes<\/li>\n\n\n\n<li>Practical Android examples<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Happy Coding!<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Continue learning modern Android development with <strong>Moiks Developer Blog<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Master Kotlin StateFlow for modern Android development. Learn MutableStateFlow, ViewModel integration, lifecycle-aware collection, immutable UI state, and Jetpack Compose with a complete practical example.<\/p>\n","protected":false},"author":1,"featured_media":161,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1,"footnotes":""},"categories":[3,333,7],"tags":[],"class_list":["post-160","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-development","category-android-tutorials","category-coroutines-flow"],"_links":{"self":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/comments?post=160"}],"version-history":[{"count":1,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/160\/revisions"}],"predecessor-version":[{"id":162,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/160\/revisions\/162"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media\/161"}],"wp:attachment":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media?parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/categories?post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/tags?post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}