Im having a bit of an odd problem with compose and...
# compose
z
Im having a bit of an odd problem with compose and data updates! For example, if I tap a button that creates some data that I then navigate to using its id; the current screen will update to reflect the new data before the transition to the new screen is done. This looks a bit quirky, kind of unexpected; the only solution that comes to mind is to somehow pause data updates during navigation .. but that doesnt really seem like a good idea. Any other ideas? 🙂
s
+1, same problem
i
collectAsStateWithLifecycle
takes a
Lifecycle.State
so you could set that to
RESUMED
(instead of its default
STARTED
) to avoid updating your state while the animation is running - the Navigation Component synchronously moves the state of a screen down to
STARTED
when you call
navigate
z
Interesting @Ian Lake, could you please point me to where that happens? I dont use the navigation component but I could use it as inspiration in my own setup 🙏🏽
i
Well, first every screen needs its own
LifecycleOwner
, which is something the Navigation Component provides for you, but the code to actually compute the updated lifecycle after a navigate call is at https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigation/navigation-runtime/src/main/java/androidx/navigation/NavController.kt;l=927
Of course, the Navigation Component is probably doing more than you might need to do - it handles dialog destinations, subgraphs (that have their own Lifecycle that is the max of all of their children), reentrant calls to navigate, etc.
z
Thank you Ian! That looks complicated, but I think I get the gist of it and how I could make it work 🙏🏽