Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose: What happens if I declare MutableState as a instance variable and use it in a composable function without remember?

The question is as follows; I have a mutable String variable declared as a instance variable of the activity. When I use it like this, it works as expected. When textfield value changes, the text value changes as well.

class MainActivity : ComponentActivity() {

  private var text = mutableStateOf("Hello!")

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
      AppTheme {
        Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
          TextField(value = text.value, onValueChange = {
            text.value = it
          })

          Text(text = text.value)
        }
      }
    }
  }
}
  1. What is the need to use remember in this case? Is it just to isolate the state to the proper composable function?
  2. Are there any disadvantages of using the mutable state as mentioned above?
like image 631
CatVsDogs Avatar asked Oct 30 '25 10:10

CatVsDogs


2 Answers

What is the need to use remember in this case?

None, remember is not needed in this case.

Is it just to isolate the state to the proper composable function?

No. remember will remember the instance created by the first composition and return that value when the function is recomposed. It serves no other purpose. Since the instance is stored in the activity there is no reason (and it is wasteful) to remember it. remember is just value memoization, it is not required to trigger recomposition. As the activity already "remembers" the value, there is no need to remember it in composition.

Are there any disadvantages of using the mutable state as mentioned above?

Yes there are but it relates to the Activity life-cycle, not Compose. When the Activity is recreated, state will revert back to its initial state (e.g. "Hello"). Activities are recreated for a variety of reasons in Android such as configuration changes (phone rotation, etc.). If you use rememberSaveable instead of a field of the Activity the state will be saved into a bundle automatically. For values in the activity you would need to save and restore these states as part of the bundle yourself. Alternately, moving this state to a view model would also make it independent of the Activity life-cycle.

These disadvantages are not Compose things, they are Android things a Compose application should be aware of. What you wrote Compose is fine with, Android is not.

like image 68
chuckj Avatar answered Nov 01 '25 01:11

chuckj


Short answer: remember is not to the variable to be able to recompose but to avoid recomposing.

Now, with this I mean that with remember, compose remembers the variable and its value is preserved across recompositions.

For example: If you have another mutable state that defines another Text block, when you change this new variable and you don't have remember, the first one is also recomposed. If you do have remember the state will be preserved and that is more efficient when you have a big application.

like image 24
Initdd Avatar answered Nov 01 '25 02:11

Initdd