`remember {}` saves X for recomposition but when composable leaves the screen X is forgotten. if I w...
m
remember {}
saves X for recomposition but when composable leaves the screen X is forgotten. if I want to save X even when composable leaves the screen I should use
rememberSaveable {}
right? it seems weird because concept of
rememberSaveable {}
is for surviving
recreating
but composable leaving the screen is not
recreation
. conceptually not the same. should I use
rememberSaveable {}
or there is some other way to handle this use case?
k
IMO it is more logical to hoist the state to a place with the lifetime you want.
👍🏽 1
👍 1
1
m
@knthmn I understand and I agree. BUT. what if you don’t need/want that state hoisted? let’s say you have list of items you want to select. yes, you probably need to do something with those items inside
ViewModel
or something and in that case you can make composables(items) in the list stateless (implementation differs from use case to use case) but what if you don’t need these items inside
ViewModel
? maybe your use case is so simple and you would introduce
ViewModel
just for saving checked state? this is all maybe and probably not good example but I hope you get a point
a
You can try Decompose, it works exactly how you expect.
c
@Marko Novakovic not an expert, but "if i want to save x even when composable leaves the screen" do you mean when you use it in a LazyColumn? Composables leaving the screen AFAIK don't lose their state. It's only if it's in a lazy container will it lose it's state. And in that case, you want to do what the first comment said and hoist the state up. Again, not an expert and so I could be completely wrong. Adam Powell could probably clear it up when he wakes up, BUT it might make sense for you to define "leaves the screen". Do you navigate to another screen? Do you mean scrolling off the screen in a Column? Do you mean scrolling off the screen in a lazy column? Do you mean off the screen as in backgrounding the app, and then process death hits. etc.
k
@Marko Novakovic can you provide a concrete example on what you want stored?
a
all the
viewModel
composable is doing is hoisting your state into
LocalViewModelStoreOwner.current
for you
the lazy layouts do indeed have items leave the composition entirely when they're no longer visible, so yes, if you don't hoist state somewhere, it won't stick around.
1
m
@knthmn I don’t have an example. I played around with it and was wondering. conceptually didn’t make sense
@Adam Powell thanks
a
rememberSaveable
can also be used inside
LazyColumn
items for not loosing the local state when the item is scrolled off the screen(for example for checked state as you mentioned). so activity recreation is just one of the use cases of
rememberSaveable
m
@Andrey Kulikov that’s the answer I was looking for. thanks!
@Andrey Kulikov and that was my test case, store variable with
remember {}
and
rememberSaveable {}
and scroll item off the screen, than bring it back to see what’s going on