Issue
So in one Compose sample by Google I noticed that they check if lifecycle is resumed before navigating between composable screens:
// In order to discard duplicated navigation events, we check the Lifecycle
if (from.lifecycleIsResumed()) {
navController.navigate("${MainDestinations.COURSE_DETAIL_ROUTE}/$newCourseId")
}
/**
* If the lifecycle is not resumed it means this NavBackStackEntry already processed a nav event.
*
* This is used to de-duplicate navigation events.
*/
private fun NavBackStackEntry.lifecycleIsResumed() =
this.lifecycle.currentState == Lifecycle.State.RESUMED
Seems all other samples don't have it and the same for nowinandroid, also I don't think docs have it as well.
Is it still relevant? Should we use it or not?
Solution
The line of code you referenced the error #456 (Simultaneous taps navigate multiple times).
If multiple items are tapped at the same time, 2 or more detail screens can be launched. This isn't necessarily a uniquely Jetpack Compose issue, as this plagues a lot of non-Jetpack Compose apps, but I'd be very interesting to see what the "correct" way to handle it is in the Compose world.
The error is resolved by checking the lifecycle status of the app:
/**
* If the lifecycle is not resumed it means this NavBackStackEntry already processed a nav event.
*
* This is used to de-duplicate navigation events.
*/
private fun NavBackStackEntry.lifecycleIsResumed() =
this.lifecycle.currentState == Lifecycle.State.RESUMED
Is it still relevant? Yes, at least personally I haven't found any changelog where they implemented it in their release.
Should we use it or not? If your goal is not to navigate to multiple destinations, yes.
Answered By - G. Ciardini
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.