Does anyone know if there is a way to keep `rememb...
# compose
i
Does anyone know if there is a way to keep
remember
state alive between calls to
movableContentOf
? I currently have a situation where I am doing something like this:
Copy code
val moveable = remember { moveableState { DrawAnItem() } }
if(visible) moveable.invoke()
The problem is that when “visible” gets toggled, all `remember`ed state inside
DrawAnItem
is reset. However, if I do this:
Copy code
val moveable = remember { moveableState { DrawAnItem() } }
if(visible) moveable.invoke()
else {
    Box(modifier = Modifier.alpha(0f)) {
        moveable.invoke()
    }
}
the `remember`ed items are properly remembered in between calls to “visible”. This obviously isn’t a very good solution, but I’m trying to figure out how to keep a composable “alive” when I know that I’m going to render it again in the not-too-distant future.
👀 1
f
The cop out answer would be to hoist your remembered state outside of the
DrawAnItem
composable 😄 But that does not really answer your question
(As a side note,
DrawAnItem
naming goes against the compose naming conventions)
i
Yes, unfortunately hoisting state isn’t possible for my use case - this is for a generic screen animation and themeing system, so “DrawAnItem”, in my codebase, would actually render one of n full screens, which are meant to encapsulate their logic completely. This isn’t “real” code, I just threw it together as a sample (hence the “DrawAnItem” not really being good naming!)
z
What kind of state are you looking to keep around? For example, LazyListState is remembered using rememberSaveable and will be restored rather than recreated if your content (DrawAnItem) is wrapped with
SaveableStateHolder.SaveableStateProvider
under a given key. Maybe this is not the answer youre looking for as well, but many navigation frameworks use this approach under the hood so I thought Id at least mention it 🙂
i
I don’t know what might be kind of state might be remembered there - I won’t be the person who’s writing the
remember
calls. I was hoping to get remember working, but I think that rememberSaveable will probably be enough. However,
rememberSaveable
wasn’t working correctly for me until I moved some of my Composable logic around (where some onDispose calls were, where the SaveableStateHolder was being used, etc), but it all appears to be working now with
rememberSaveable
. Thanks for your help.