On Android I am using `LifecycleEventEffect(Lifecy...
# compose
p
On Android I am using
LifecycleEventEffect(Lifecycle.Event.ON_CREATE)
inside a lazycolumn. Some subcomponents use it. I notice the onCreate() event gets called multiple times as the lazycolumn scroll up and down. I would expect this from the onStart() event as composables get attached/detached to/from the composition but not the onCreate() event Is this the expected behavior or a bug?
j
Observers of the lifecycle are brought up to the current state when they start listening
As I type this, though, I'm reading that you are using the event effect though... I have never used that! Only the state one.
Nah it's based around the same thing. It adds an observer and then I would expect that observer to see all events which lead to the current state
p
I see. So it means it will fire the previous events too. I will change to compare against the current State better. Thanks
s
Composables are attached / detached from composition
While slightly offtopic, this is not quite the correct model here. Technically each item in a lazy layout is recreated with nodes being reused. This means that every state is lost and every effect will be triggered as if it was never executed before. This is substantially different from what recyclerview did with views, since there state was preserved unless explicitly cleared.
p
I noticed that. I ended up with some workaround for now. Basically a connector between the Composable and the ViewModel, the connector tracks the Lifecycle events so it ensures no duplicates are dispatched.
Here is the gist: https://gist.github.com/pablichjenkov/21691f49ed386ba92d58a5fb0fef1588 Basically a state machine to filter and re-dispatch the lifecycle events. Pretty stateful but working good for now, until being able to refactor the components startup
Feel free to destroy it