dimsuz
05/31/2021, 8:03 PMComposeView
in an ordinary view hierarchy. See the snippet in the thread.dimsuz
05/31/2021, 8:06 PMclass MyActivity {
private var state by mutableStateOf("hello")
fun onCreate() {
setContent {
val s = remember { state }
SomeComposable(s.value)
}
}
fun onResume() {
state = "world"
}
}
will SomeComposable
get "world" eventually?
I tried this and it doesn't, but maybe I do something wrong?dimsuz
05/31/2021, 8:12 PMremember
would recalculate it's lambda only if I did s.setValue("hello")
, the way I did this above bypasses any 'remember-wrapping.
But maybe there's some way to mutate the state outside of composable context? Asking because I'm trying to adapt a pre-compose classes to some intermediate scheme.
For now I settled on having a MutableStateFlow
to which I subscribe inside setContent
and to which I post inside onResume
Adam Powell
05/31/2021, 8:12 PMdimsuz
05/31/2021, 8:13 PMAdam Powell
05/31/2021, 8:13 PMvar state by mutableStateOf("hello")
the type of state
is String
, not State<String>
Adam Powell
05/31/2021, 8:13 PMstate
appears in your code it's evaluating the current value of state
Adam Powell
05/31/2021, 8:14 PMval s = remember { state }
means that s
will remember the initial value of state
and not see any changes; it's a copy of the string reference that was evaluated at initial compositionAdam Powell
05/31/2021, 8:15 PMclass MyActivity {
private var state by mutableStateOf("hello")
fun onCreate() {
setContent {
SomeComposable(state)
}
}
fun onResume() {
state = "world"
}
}
dimsuz
05/31/2021, 8:17 PMString
.
Skip remembering another reference to something that already exists in your scope and just go straight to the source:Wait! Would this work?! I need to read Zach's article on
remember
and state
once again :)dimsuz
05/31/2021, 8:21 PMmutableStateOf
as having a magic little remember
inside it?
I guess I should also read the article on snapshots.
I'm in a phase when I understand the concepts separately, but they still didn't click together. Until they do I usually ask some really stupid quesitons 🙂Adam Powell
05/31/2021, 8:23 PMCan I think ofÂNo, the two are completely orthogonal. This is why you see as having a magic littleÂmutableStateOf
 inside it?remember
remember { mutableStateOf(...) }
often.Adam Powell
05/31/2021, 8:24 PMremember
inside it is MyActivity
- you've declared state
as a property of the activity class itself, so it persists there instead of as part of the compositionAdam Powell
05/31/2021, 8:25 PMremember {}
is a little like declaring a private instance property of a @Composable fun
dimsuz
05/31/2021, 8:27 PMmutableStateOf()
is that wherever I keep it (in Activity or in composition or etc), if I reference it inside a composition, and call its setter from wherever else, then it will cause recomposition?Adam Powell
05/31/2021, 8:31 PMdimsuz
05/31/2021, 8:35 PM