What should I do if displaying a few items in a `L...
# compose
f
What should I do if displaying a few items in a
LazyColumnItems
lags the screen for like 0.5 seconds when displayed? Is this perhaps because this is a preview, or do I have some way of profiling where the device spends its time?
t
It should not lag in any way. Maybe you are displaying very big images?
f
not images, just some text with some draggable modifiers
t
I think we need more information to help you. Can you provide some code?
f
there is quite a bit of stuff
I'll try to solve a problem I have now where
remember{}
doesn't remember at all and then try to simplify it
This is a very frustrating thing i've found about compose, because it doesn't use normal OOP semantics but instead hidden magics it's unclear when how or why something is saved in
remember{}
and the likes
t
for me val variable by state { } works most of the time better
And it will be deleted wenn the Composable is removed from the view
f
Is there a way to keep the state even when the composable is removed from the view? so that the state remains when you navigate back and forth
t
Yes just define the variable not inside of the view.
Copy code
var state by mutableStateOf(initialState)
f
I guess that's one way
Isn't
savedInstanceState
supposed to work too? If it survives process death it should survive switching views too
t
yes you are right. I did not used it yet because in the past it had many problems.
But now it should work as expceted
f
I tried
savedInstanceState
, seems like it doesn't work at all
Doesn't survive process death or switching views
h
remember
is confusing because it feels like it's supposed to remember a
variable
but it actually remembers the result of a calculation. So if your composable gets recomposed because of a dependency change,
remember
will trigger again but won't invoke the callback function. So if you've made changes to the local variable that was initialized by
remember
, it will get re-set by initially remembered value