I have a question, What's the difference between ...
# compose
c
I have a question, What's the difference between
Copy code
val interactionSource = remember { MutableInteractionSource() }
and
Copy code
val interactionSource by remember { MutableInteractionSource() }
and
Copy code
val interactionSource by rememberSavable { MutableInteractionSource() }
???
j
First one returns State<MutableInteractionSource> which, if you want to read/save you have to use like
interactionSource.value = <something>
Second one is just a simple delegate that makes working with that easier, like
interactionSource = <something>
The last one is exactly the same as second, but it stores the state across android lifecycle, so it will be save when used rotates the screen or something else that will trigger configuration change
c
As I remember, In the codelab
= remember { }
is not recommended. But is this still useful?
Maybe for flow?
j
Why not recommended? This is a valid usage for many usecases
if you don't care about saving that particular state simple remember will be faster
c
Aha, I see! thank you 🙂
l
MutableInteractionSource is not a State
z
Yea the second 2 won’t compile because
MutableInteractionSource
doesn’t define a
getValue
method.
Only
State
does that (and
MutableState
additionally defines
setValue
) Sonly
m
Also, i'm not convinced that an InteractionSource would be saveable anyway. Things give to a rememberSaveable need to generally be serializable in some form, or you need to pass a Saver to it to tell it what to do. I'm not convinced MutableSharedFlow meets this requirements. It's basically a wrapper for a MutableSharedFlow.
z
Yea it doesn’t make sense to save an MIS, and i don’t think it supports it.
b
I am probably doing something wrong, but I have a val x = remember {...) SomeComposeThatChangesX(x) SomeComposeThatShowsADifferentViewOnX(x) I know this could probably be done via a viewModel with 2 and 3, but I think 1 is easier
So x.value can be used in SomeComposeThatChangesX