Status Update
Comments
il...@google.com <il...@google.com>
tr...@gmail.com <tr...@gmail.com> #2
Another solution idea would be to use "onAppear" modifier to allow each composable have an individual event handler.
For example:
```
Column(
modifier = Modifier
.fillMaxSize()
.onAppear {
viewModel.loadItems()
}
) {
...
}
```
a....@gmail.com <a....@gmail.com> #3
sj...@gmail.com <sj...@gmail.com> #4
This would be really appreciated. Especially when dealing with legacy Android views wrapped in an AndroidView
that require specific methods to be called on certain lifecycle states, like a camera/scanner view for example.
sj...@gmail.com <sj...@gmail.com> #5
As a workaround I created this function utilizing produceState()
:
@Composable
fun currentLifecycleState(): State<Lifecycle.State> {
val lifecycleOwner = LocalLifecycleOwner.current
return produceState(
initialValue = Lifecycle.State.INITIALIZED,
) {
val observer = object : DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
value = owner.lifecycle.currentState
}
override fun onStart(owner: LifecycleOwner) {
value = owner.lifecycle.currentState
}
override fun onResume(owner: LifecycleOwner) {
value = owner.lifecycle.currentState
}
override fun onPause(owner: LifecycleOwner) {
value = owner.lifecycle.currentState
}
override fun onStop(owner: LifecycleOwner) {
value = owner.lifecycle.currentState
}
override fun onDestroy(owner: LifecycleOwner) {
value = owner.lifecycle.currentState
}
}
lifecycleOwner.lifecycle.addObserver(observer)
awaitDispose { lifecycleOwner.lifecycle.removeObserver(observer) }
}
}
sa...@google.com <sa...@google.com>
ap...@google.com <ap...@google.com> #6
Branch: androidx-main
commit c1f4d85af7a690c310fc9dd60e27654ba2f4896e
Author: sanura <sanura@google.com>
Date: Tue Mar 07 01:16:59 2023
Add Lifecycle extension for composable collectAsState()
Adding a Lifecycle composable extension for developers
wanting to directly expose Lifecycle.State as a Compose State.
RelNote: "`Lifecycle.collectAsState()` extension added to
directly expose `Lifecycle.State` as Compose `State`. This
is equivalent (and a shorter alternative) to
`lifecycle.currentStateFlow.collectAsState()`."
Bug: 235529345
Test: lifecycleCollectAsState()
Change-Id: I1101507a270980554046f7505b356c9c923ad48e
M lifecycle/lifecycle-runtime-compose/api/current.txt
M lifecycle/lifecycle-runtime-compose/api/public_plus_experimental_current.txt
M lifecycle/lifecycle-runtime-compose/api/restricted_current.txt
A lifecycle/lifecycle-runtime-compose/src/androidTest/java/androidx/lifecycle/compose/LifecycleExtTest.kt
A lifecycle/lifecycle-runtime-compose/src/main/java/androidx/lifecycle/compose/LifecycleExt.kt
lm...@gmail.com <lm...@gmail.com> #7
lm...@gmail.com <lm...@gmail.com> #8
lm...@gmail.com <lm...@gmail.com> #9
ap...@google.com <ap...@google.com> #10
Branch: androidx-main
commit e9afa3a28b32897fef6717fea079c6296f3a2e5a
Author: sanura <sanura@google.com>
Date: Tue Apr 25 01:07:17 2023
Create LifecycleEventEffect composable
Adding LifecycleEventEffect composable API to
be used when running SideEffects based on a
particular Lifecycle.Event.
RelNote: "`LifecycleEventEffect` API has been
added to run side effects based on `Lifecycle.Event`."
Test: LifecycleEventEffectTest
Bug: 235529345
Change-Id: Ic9794d7e59b1a66cc7f31cbaebed9b3d3dee5dd3
M lifecycle/lifecycle-runtime-compose/api/current.txt
M lifecycle/lifecycle-runtime-compose/api/public_plus_experimental_current.txt
M lifecycle/lifecycle-runtime-compose/api/restricted_current.txt
M lifecycle/lifecycle-runtime-compose/src/androidTest/java/androidx/lifecycle/compose/CollectAsStateWithLifecycleTests.kt
A lifecycle/lifecycle-runtime-compose/src/androidTest/java/androidx/lifecycle/compose/LifecycleEffectTest.kt
M lifecycle/lifecycle-runtime-compose/src/androidTest/java/androidx/lifecycle/compose/LifecycleExtTest.kt
A lifecycle/lifecycle-runtime-compose/src/main/java/androidx/lifecycle/compose/LifecycleEffect.kt
ap...@google.com <ap...@google.com> #11
Branch: androidx-main
commit 25a2002ab933ea551295483aa3f7e278d60dbe75
Author: sanura <sanura@google.com>
Date: Wed May 03 20:20:56 2023
Create LifecycleResumeEffect composable
Adding LifecycleResumeEffect composable API to be used when
running SideEffects based on ON_RESUME/ON_PAUSE event callbacks.
RelNote: "`LifecycleResumeEffect` API has been added to run side
effects based on both `Lifecycle.Event.ON_RESUME` and
`Lifecycle.Event.ON_PAUSE` event callbacks."
Test: lifecycleResumeEffectTest
Bug: 235529345
Change-Id: I603860f21eb6a433c909d7ae6f8120f6d23179aa
M lifecycle/lifecycle-runtime-compose/api/current.txt
M lifecycle/lifecycle-runtime-compose/api/public_plus_experimental_current.txt
M lifecycle/lifecycle-runtime-compose/api/restricted_current.txt
M lifecycle/lifecycle-runtime-compose/src/androidTest/java/androidx/lifecycle/compose/LifecycleEffectTest.kt
M lifecycle/lifecycle-runtime-compose/src/main/java/androidx/lifecycle/compose/LifecycleEffect.kt
ap...@google.com <ap...@google.com> #12
Branch: androidx-main
commit 2b090a54b8182927eb0e96062e0a14d065a8561a
Author: sanura <sanura@google.com>
Date: Tue May 02 19:53:55 2023
Create LifecycleStartEffect composable
Adding LifecycleStartEffect composable API to be used when
running SideEffects based on ON_START/ON_STOP event callbacks.
RelNote: "`LifecycleStartEffect` API has been added to run side
effects based on `Lifecycle.Event.ON_START` and
`Lifecycle.Event.ON_STOP` event callbacks."
Test: lifecycleStartEffectTest
Bug: 235529345
Change-Id: I5a8d1e6f152b7083e086146ba97044c4b454db84
M lifecycle/lifecycle-runtime-compose/api/current.txt
M lifecycle/lifecycle-runtime-compose/api/public_plus_experimental_current.txt
M lifecycle/lifecycle-runtime-compose/api/restricted_current.txt
M lifecycle/lifecycle-runtime-compose/src/androidTest/java/androidx/lifecycle/compose/LifecycleEffectTest.kt
M lifecycle/lifecycle-runtime-compose/src/main/java/androidx/lifecycle/compose/LifecycleEffect.kt
st...@gmail.com <st...@gmail.com> #13
Is there a target API version where this may be released?
il...@google.com <il...@google.com> #14
This is still in progress for inclusion in Lifecycle 2.7.0-alpha01.
ap...@google.com <ap...@google.com> #15
Branch: androidx-main
commit ad273fa4882a38a5a2f72004044ce3173b2c06a9
Author: sanura <sanura@google.com>
Date: Fri May 05 02:02:59 2023
Add samples for LifecycleEffects
Adding samples for LifecycleEventEffect,
LifecycleStartEffect, and LifecycleResumeEffect.
RelNote: "All composables in `LifecycleEffect` now
link sample usages."
Test: ./gradlew bOS
Bug: 235529345
Change-Id: Ib4786f4b586cf9390f3dbe6f3b0fc86c60267a86
M lifecycle/lifecycle-runtime-compose/samples/src/main/java/androidx/lifecycle/compose/samples/LifecycleComposeSamples.kt
M lifecycle/lifecycle-runtime-compose/src/main/java/androidx/lifecycle/compose/LifecycleEffect.kt
sa...@google.com <sa...@google.com>
lm...@gmail.com <lm...@gmail.com> #16
ka...@gmail.com <ka...@gmail.com> #17
When will this be released?
na...@google.com <na...@google.com> #18
The following release(s) address this bug.It is possible this bug has only been partially addressed:
androidx.lifecycle:lifecycle-runtime-compose:2.7.0-alpha01
sj...@gmail.com <sj...@gmail.com> #19
Just want to say that Lifecycle 2.7.0
stable has been finally released. Thank you very much 🙏🏼
ey...@gmail.com <ey...@gmail.com> #20
jb...@google.com <jb...@google.com> #21
Yes, those are the Lifecycle.State
s that reflect those particular Lifecycle.Event
s. Please take a look at the
ey...@gmail.com <ey...@gmail.com> #22
jb...@google.com <jb...@google.com> #23
Receiving a state from the flow would be the signal that a change has occurred and the new state is that particular change. There are cases where you don't care how you get to a state, just that you are there. We also offer Lifecycle.Event
s.
il...@google.com <il...@google.com> #24
If you look at the Lifecycle.asFlow()
to get a Flow of Lifecycle.Event
) and one for Lifecycle State (where you can use Lifecycle.currentStateFlow
to get the current Lifecycle.State
).
Make sure you are using the right API.
ey...@gmail.com <ey...@gmail.com> #25
il...@google.com <il...@google.com> #26
Lifecycle.asFlow()
works outside of Compose too and gives you Lifecycle.Event
instances as they occur. That's why that API exists separately from currentStateFlow
.
Description
Component used: Lifecycle
Version used: 2.5.0-rc01
Reacting to a Lifecycle.State changes is currently fairly complicated in a Jetpack Compose based app, requiring a with registering a
DisposableEffect
based approachLifecycleObserver
with theLocalLifecycleOwner
.Ideally, this would come in two forms:
Lifecycle.State
asState
, perhaps something likeLocalLifecycleOwner.current.lifecycle.currentStateFlow.collectAsState()
LifecycleEventEffect
that would only run the effect when theLifecycle.State
changes.