We have an `AndroidView` which reads from a lambda...
# compose-android
l
We have an
AndroidView
which reads from a lambda passed into the function, which is stable
Copy code
AndroidView(factory = { context -> 
    onViewCreated()
    ...
  }
)
However this is causing the factory function to be called, and the underlying view to be re-created on every configuration change. The lambda is ultimately created this way:
Copy code
val onViewEvent = remember(viewModel) { viewModel::onViewEvent }
val onViewCreated = { onViewEvent(SomeViewEvent) }
The problem I'm seeing is that
remember
doesn't survive configuration changes, so
onViewEvent
gets remember'd again with a new reference (even though the viewModel instance is the same) and we can't use
rememberSaveable
with lambdas (and I'm not sure it feels right to do so!) Has anyone dealt with this before and figured out a solution?
j
Do
remember {}
get preserved during configuration changes? I thought the entire compositions gets recreated since the Activity is getting recreated? Have you disabled that functionality first?
Can you put a
println
statement in your Activities
onCreate(Bundle)
and see if it get printed every time you rotate your device?
u
@luke_c the factory function is called on every configuration change because of the configuration change (re-creating Activity & composition), not because of
onViewEvent
changing. Is that what your question is about?
☝🏻 1
👍 1