<@UHAJKUSTU> what is the correct way to handle com...
# decompose
e
@Arkadii Ivanov what is the correct way to handle component lifecycle? I added this to my component:
Copy code
init {
        lifecycle.doOnResume {
            store.accept(Intent.Refresh)
        }
    }
but I don’t get the event triggered when the component is brought to front via
navigation.bringToFront
a
This should be the correct way. What platform are you trying on?
e
Android. I don’t understand what is wrong here. I’m trying to do that for the first time.
do I need to somehow bind the component to the platform-specific lifecycle?
Every child component needs its own ComponentContext. Never pass parent's ComponentContext to children
e
Yes, I will check, but I am passing child context every time.
a
Firstly, check if the root component received lifecycle events at all. If it is, then check that you never pass part ComponentContext to children. If not, then the issue is in the integration point.
e
The parent component recieves the events correctly. But child doesn’t. It receives onResume once and never again. I’m creating it with this code:
Copy code
private val previousPlacesComponent by lazy { createPreviousPlacesComponent(childContext("PreviousPlaces")) }
a
In this case - it's by design, since it's a permanent child.
You can create the component in the childFactory function instead, and pass the ComponentContext provided there.
e
so is the issue related to the fact that instead of using context from
childFactory
I am using
childContext()
?
Is the component recreated each time it is brought to front?
btw that solved the issue, thank you! but I want to understand how it works under the hood. I expected that regardless how the component was created if it got a child context it should receive the events along with navigation
a
The only way for a component to receive lifecycle events is through
ComponentContext
. Only the owner (the creator) of the
ComponentContext
instance can control its lifecycle. When you create
ComponentContext
using
childContext
function, then the lifecycle is the as as the parent. Though, the lifecycle can be also controlled manually as described in the docs. When you use navigation, then the navigation creates and controls the child
ComponentContext
.
Is the component recreated each time it is brought to front?
Only if the configuration has changed (i.e. not
equal
to the previous one).
e
Thanks a lot!
👍 1