Bird
Raised Fist0
Android Kotlinmobile~15 mins

remember and mutableStateOf in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Counter Screen
A simple screen that shows a number and a button to increase it. The number remembers its value even if the screen recomposes.
Target UI
-------------------
|   Counter: 0    |
|                 |
|  [ Increase ]   |
-------------------
Display a number starting at 0
Add a button labeled 'Increase'
When the button is tapped, increase the number by 1
Use remember and mutableStateOf to keep the number state
The number should update on the screen immediately after tapping
Starter Code
Android Kotlin
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun CounterScreen() {
    // TODO: Add state variable here

    Column(
        modifier = Modifier.fillMaxSize().padding(16.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(text = "Counter: 0", style = MaterialTheme.typography.headlineMedium)
        Spacer(modifier = Modifier.height(16.dp))
        Button(onClick = { /* TODO: Increase counter */ }) {
            Text("Increase")
        }
    }
}

@Preview(showBackground = true)
@Composable
fun CounterScreenPreview() {
    CounterScreen()
}
Task 1
Task 2
Task 3
Solution
Android Kotlin
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.foundation.layout.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun CounterScreen() {
    var counter by remember { mutableStateOf(0) }

    Column(
        modifier = Modifier.fillMaxSize().padding(16.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(text = "Counter: $counter", style = MaterialTheme.typography.headlineMedium)
        Spacer(modifier = Modifier.height(16.dp))
        Button(onClick = { counter++ }) {
            Text("Increase")
        }
    }
}

@Preview(showBackground = true)
@Composable
fun CounterScreenPreview() {
    CounterScreen()
}

We use remember with mutableStateOf to create a state variable counter that survives recompositions of the CounterScreen composable. The by keyword with var counter allows us to read and write the state value directly.

The Text composable shows the current value of counter. When the Button is clicked, we increase counter by 1. Because counter is a state, Compose automatically updates the UI to show the new value.

This pattern is common in Jetpack Compose to keep UI state simple and reactive.

Final Result
Completed Screen
-------------------
|   Counter: 0    |
|                 |
|  [ Increase ]   |
-------------------
Tapping the 'Increase' button increments the number shown after 'Counter:' by 1 immediately.
Stretch Goal
Add a 'Reset' button that sets the counter back to 0
💡 Hint
Add another Button below the 'Increase' button with onClick setting counter = 0

Practice

(1/5)
1. What is the main purpose of remember in Jetpack Compose?
easy
A. To keep a value during recompositions so it doesn't reset
B. To create a mutable state that updates the UI
C. To launch a coroutine for background tasks
D. To define a new composable function

Solution

  1. Step 1: Understand what recomposition means

    Recomposition happens when the UI redraws, and values can reset unless preserved.
  2. Step 2: Identify the role of remember

    remember keeps a value alive during recompositions to avoid resetting.
  3. Final Answer:

    To keep a value during recompositions so it doesn't reset -> Option A
  4. Quick Check:

    remember = keeps value during redraws [OK]
Hint: remember keeps values stable across UI redraws [OK]
Common Mistakes:
  • Confusing remember with mutableStateOf
  • Thinking remember creates mutable state
  • Assuming remember launches background tasks
2. Which of the following is the correct syntax to create a mutable state variable named count initialized to 0 using remember and mutableStateOf?
easy
A. val count = remember { mutableStateOf(0) }
B. var count = mutableStateOf(remember(0))
C. val count = mutableStateOf(remember { 0 })
D. var count = remember.mutableStateOf(0)

Solution

  1. Step 1: Recall the correct order of functions

    remember wraps mutableStateOf to keep state across recompositions.
  2. Step 2: Check syntax correctness

    val count = remember { mutableStateOf(0) } correctly uses val count = remember { mutableStateOf(0) }. Others misuse function calls or syntax.
  3. Final Answer:

    val count = remember { mutableStateOf(0) } -> Option A
  4. Quick Check:

    remember wraps mutableStateOf syntax [OK]
Hint: remember { mutableStateOf(value) } is the correct pattern [OK]
Common Mistakes:
  • Swapping order of remember and mutableStateOf
  • Using var instead of val incorrectly
  • Calling remember or mutableStateOf as properties
3. Given the code snippet:
var count = remember { mutableStateOf(0) }
count.value += 1
println(count.value)
What will be printed after this code runs once?
medium
A. 0
B. Compilation error
C. null
D. 1

Solution

  1. Step 1: Understand initial value of count

    count is a mutable state holding 0 initially.
  2. Step 2: Increment the value and print

    count.value += 1 increases value to 1, so printing count.value outputs 1.
  3. Final Answer:

    1 -> Option D
  4. Quick Check:

    mutableStateOf value increments correctly [OK]
Hint: mutableStateOf value accessed with .value [OK]
Common Mistakes:
  • Printing count instead of count.value
  • Forgetting to use .value to access state
  • Assuming count is a simple Int
4. What is wrong with this code snippet?
var count = mutableStateOf(0)
count = remember { mutableStateOf(1) }
medium
A. Cannot assign remember result to a var
B. Using var instead of val for state causes errors
C. Remember must be called inside a composable function
D. mutableStateOf cannot be reassigned

Solution

  1. Step 1: Identify where remember can be used

    remember can only be called inside a composable function context.
  2. Step 2: Analyze the snippet context

    The snippet shows remember called outside composable, which is invalid.
  3. Final Answer:

    Remember must be called inside a composable function -> Option C
  4. Quick Check:

    remember requires composable context [OK]
Hint: remember only works inside @Composable functions [OK]
Common Mistakes:
  • Calling remember outside composable functions
  • Confusing var and val usage
  • Thinking mutableStateOf cannot be reassigned
5. You want to create a counter button that increments a number each time it is clicked and keeps the count even if the screen recomposes. Which code snippet correctly implements this behavior?
hard
A. @Composable fun Counter() { val count = mutableStateOf(0) Button(onClick = { count.value++ }) { Text("Count: ${count.value}") } }
B. @Composable fun Counter() { var count by remember { mutableStateOf(0) } Button(onClick = { count++ }) { Text("Count: $count") } }
C. @Composable fun Counter() { var count = 0 Button(onClick = { count++ }) { Text("Count: $count") } }
D. @Composable fun Counter() { val count = remember { 0 } Button(onClick = { count++ }) { Text("Count: $count") } }

Solution

  1. Step 1: Ensure state is remembered and mutable

    Using remember { mutableStateOf(0) } with var count by delegates state correctly.
  2. Step 2: Check button click updates state and UI

    Incrementing count++ updates state and triggers UI recomposition.
  3. Step 3: Analyze other options

    @Composable fun Counter() { val count = mutableStateOf(0) Button(onClick = { count.value++ }) { Text("Count: ${count.value}") } } lacks remember, so state resets on recomposition. @Composable fun Counter() { var count = 0 Button(onClick = { count++ }) { Text("Count: $count") } } uses simple var without state, so UI won't update. @Composable fun Counter() { val count = remember { 0 } Button(onClick = { count++ }) { Text("Count: $count") } } remembers an Int, not mutable state, so increment fails.
  4. Final Answer:

    @Composable fun Counter() { var count by remember { mutableStateOf(0) } Button(onClick = { count++ }) { Text("Count: $count") } } -> Option B
  5. Quick Check:

    remember + mutableStateOf + var by delegate = correct counter [OK]
Hint: Use remember { mutableStateOf() } with var by for stateful UI [OK]
Common Mistakes:
  • Not using remember causes state reset on recomposition
  • Using simple var without state prevents UI updates
  • Forgetting to use mutableStateOf for mutable state
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard