@@ -18,8 +18,13 @@ import androidx.lifecycle.LifecycleRegistry
18
18
@Composable internal fun rememberChildLifecycleOwner (
19
19
parentLifecycle : Lifecycle = LocalLifecycleOwner .current.lifecycle
20
20
): LifecycleOwner {
21
- val lifecycleOwner = remember {
22
- ComposeLifecycleOwner .installOn(parentLifecycle)
21
+ val owner = remember {
22
+ ComposeLifecycleOwner .installOn(
23
+ initialParentLifecycle = parentLifecycle
24
+ )
25
+ }
26
+ val lifecycleOwner = remember(parentLifecycle) {
27
+ owner.apply { updateParentLifecycle(parentLifecycle) }
23
28
}
24
29
return lifecycleOwner
25
30
}
@@ -53,12 +58,16 @@ import androidx.lifecycle.LifecycleRegistry
53
58
* - By integrating with Compose's lifecycle, it ensures that resources are properly released when
54
59
* the composable leaves the composition.
55
60
*
56
- * @param parentLifecycle The parent [Lifecycle] with which this lifecycle owner should synchronize.
61
+ * @param initialParentLifecycle The parent [Lifecycle] with which this lifecycle owner should
62
+ * synchronize with initially. If new parent lifecycles are provided, they should be passed to
63
+ * [updateParentLifecycle].
57
64
*/
58
65
private class ComposeLifecycleOwner (
59
- private val parentLifecycle : Lifecycle
66
+ initialParentLifecycle : Lifecycle
60
67
) : LifecycleOwner, RememberObserver, LifecycleEventObserver {
61
68
69
+ private var parentLifecycle: Lifecycle = initialParentLifecycle
70
+
62
71
private val registry = LifecycleRegistry (this )
63
72
override val lifecycle: Lifecycle
64
73
get() = registry
@@ -79,6 +88,12 @@ private class ComposeLifecycleOwner(
79
88
}
80
89
}
81
90
91
+ fun updateParentLifecycle (lifecycle : Lifecycle ) {
92
+ parentLifecycle.removeObserver(this )
93
+ parentLifecycle = lifecycle
94
+ parentLifecycle.addObserver(this )
95
+ }
96
+
82
97
override fun onStateChanged (
83
98
source : LifecycleOwner ,
84
99
event : Event
@@ -87,16 +102,16 @@ private class ComposeLifecycleOwner(
87
102
}
88
103
89
104
companion object {
90
- fun installOn (parentLifecycle : Lifecycle ): ComposeLifecycleOwner {
91
- return ComposeLifecycleOwner (parentLifecycle ).also {
105
+ fun installOn (initialParentLifecycle : Lifecycle ): ComposeLifecycleOwner {
106
+ return ComposeLifecycleOwner (initialParentLifecycle ).also {
92
107
// We need to synchronize the lifecycles before the child ever even sees the lifecycle
93
108
// because composes contract tries to guarantee that the lifecycle is in at least the
94
109
// CREATED state by the time composition is actually running. If we don't synchronize
95
110
// the lifecycles right away, then we break that invariant. One concrete case of this is
96
111
// that SavedStateRegistry requires its lifecycle to be CREATED before reading values
97
112
// from it, and consuming values from an SSR is a valid thing to do from composition
98
113
// directly, and in fact AndroidComposeView itself does this.
99
- parentLifecycle .addObserver(it)
114
+ initialParentLifecycle .addObserver(it)
100
115
}
101
116
}
102
117
}
0 commit comments