I need to do something when a screen is shown (nav...
# compose
m
I need to do something when a screen is shown (navigated to, or navigated back to, or the user returned from another app, etc.), which I would normally do in
onStart
. However I'm trying to build a full Compose app and using Compose Navigation, so the Activity lifecycle events not longer relate to the screens in the Compose world. What would be the correct alternative?
i
Navigation Compose gives each screen its own
Lifecycle
, so the
LocalLifecycleOwner
being used there isn't the activity if you are on one of the screens inside a
NavHost
m
Ohh I see. I saw that I can listen for the lifecycle events but thought they wouldn't be useful as the Activity is resumed the entire time I'm navigating around. Thank you.
i
FWIW, this upcoming change will allow you to write:
Copy code
LifecycleEventEffect(Lifecycle.Event.ON_START) {
  // Do your onStart side effect here
}
So it'll be a lot easier in just a few weeks 🙂
m
Sweet, I borrowed the implementation from that change and it works great, thanks. When it gets released I can just delete my copy. 😄
d
Awesome!!
i
There's a couple of other neat things going into the overarching bug - a
lifecycle.currentStateAsState
which converts
Lifecycle.State
into observable Compose State (e.g., it will automatically trigger recomposition when the Lifecycle.State changes and the upcoming
LifecycleStartEvent
and
LifecycleResumeEvent
that give
DisposableEffect
style pairs of `onStart`/`onStop` and `onResume`/`onPause` calls for automatic cleanup style work
p
Would that deprecate the
collectAsStateWithLifecycle
? Or perhaps not deprecate it but change the internal implementation of it? Last time I checked, current implementation relied on listening the lifecycle events and starting/canceling a collecting job when start/stop respectively.
i
No, no changes planned there - that API is built upon
repeatOnLifecycle
, which we're happy with the implementation with already (and doesn't depend on Compose, so can't use most of what we're building at this level)
p
Makes sense
k
This looks really nice. I was waiting for this commit to be released https://github.com/androidx/androidx/commit/b75b447af3d27aed4b4bc5a826e6e92cd0aaff34 but this would be even better. Any plans for the release? Seems like there were no releases for the lifecycle library since march
c
Was this ever released? my google-fu is failing me
k
Nope, and no info on when it will be released https://issuetracker.google.com/issues/235529345?pli=1
i
In Lifecycle 2.7.0-alpha01, just like the issue says
c
yeah. just saw that in the issuetracker link. Thanks!