admin管理员组文章数量:1356294
My routes are defined like this:
@Serializable
sealed interface Routes {
@Serializable
sealed interface TopLevel: Routes {
@Serializable
data object Home : TopLevel
@Serializable
data object Journal : TopLevel
@Serializable
data object Meals : TopLevel
@Serializable
data object Workouts : TopLevel
@Serializable
data object Profile : TopLevel
}
@Serializable
data object Onboarding : Routes
@Serializable
data object Settings : Routes
}
I want to check if my current route is of type Routes.TopLevel
. I tried this, but my understanding is that it checks if the Route is Routes.TopLevel
and not one of its derivatives.
val navHostController = rememberNavController()
val currentBackStackEntry by navHostController.currentBackStackEntryAsState()
val currentDestination = currentBackStackEntry?.destination
val isTopLevelRoute = currentDestination?.isRoute<Routes.TopLevel>() == true
// NavDestinationExt.kt
inline fun <reified T : Routes> NavDestination.isRoute(): Boolean =
this.hierarchy.any { it.hasRoute<T>() }
My routes are defined like this:
@Serializable
sealed interface Routes {
@Serializable
sealed interface TopLevel: Routes {
@Serializable
data object Home : TopLevel
@Serializable
data object Journal : TopLevel
@Serializable
data object Meals : TopLevel
@Serializable
data object Workouts : TopLevel
@Serializable
data object Profile : TopLevel
}
@Serializable
data object Onboarding : Routes
@Serializable
data object Settings : Routes
}
I want to check if my current route is of type Routes.TopLevel
. I tried this, but my understanding is that it checks if the Route is Routes.TopLevel
and not one of its derivatives.
val navHostController = rememberNavController()
val currentBackStackEntry by navHostController.currentBackStackEntryAsState()
val currentDestination = currentBackStackEntry?.destination
val isTopLevelRoute = currentDestination?.isRoute<Routes.TopLevel>() == true
// NavDestinationExt.kt
inline fun <reified T : Routes> NavDestination.isRoute(): Boolean =
this.hierarchy.any { it.hasRoute<T>() }
Share
Improve this question
asked Mar 28 at 16:34
mainrsmainrs
631 silver badge5 bronze badges
1 Answer
Reset to default 0What you need is not possible in jetpack compose navigation.
The library doesn't store the type of passed object or class as a route.
Here is the code from Jetpack compose library:
public fun <T : Any> navigate(
route: T,
navOptions: NavOptions? = null,
navigatorExtras: Navigator.Extras? = null
) {
val finalRoute : String = generateRouteFilled(route)
navigate(
NavDeepLinkRequest.Builder.fromUri(createRoute(finalRoute).toUri()).build(),
navOptions,
navigatorExtras
)
}
the route passed as a parameter is converted to a String
using generateRouteFilled
function. and never used afterward.
On the other hand, when building the navgraph, only a navigator, id, and route (string) are stored.
public open class NavDestinationBuilder<out D : NavDestination>
internal constructor(
protected val navigator: Navigator<out D>,
@IdRes public val id: Int,
public val route: String?
)
That ID is used to determine whether or not a route is generated using a type by getting hashcode of it's serializer. So it's impossible to retrieve the actual type.
A similar solution:
If your inner destinations (in your case, TopLevel
) represents a nested graph, you can nest them inside a graph and check that the graph is in the current backstack.
本文标签:
版权声明:本文标题:kotlin - How to check if the current route has a parent interface using type-safe navigation in Jetpack Compose? - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744024793a2577852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论