简体   繁体   English

"如何处理 Jetpack Compose 中的导航?"

[英]How to handle navigation in Jetpack Compose?

In Jetpack Compose, how is navigation supposed to be done?在 Jetpack Compose 中,导航应该如何完成? All (and there aren't many) examples (including the official sample from Google) use sealed classes and loading new screens in reaction to observing the change in the current screen.所有(并且没有很多)示例(包括来自 Google 的官方示例)都使用密封类并加载新屏幕以响应观察当前屏幕的变化。 This does (sort of) work, but provides no navigation backstack, and the phone's back button is totally unaware, just closes the app instead of going back to the previous screen.这确实(有点)工作,但不提供导航后台堆栈,并且手机的后退按钮完全不知道,只是关闭应用程序而不是返回上一个屏幕。 Is this supposed to somehow converge with the navigation component from AndroidX - but it's XML based, and Compose is all about moving away from XML?这是否应该以某种方式与 AndroidX 的导航组件融合——但它是基于 XML 的,而 Compose 完全是为了远离 XML? Or is there a brand new navigation concept coming, perhaps similar to SwiftUI (navigationlink, etc)?还是有一个全新的导航概念即将到来,可能类似于 SwiftUI(navigationlink 等)? This seems to be one of the biggest roadblocks - as without navigation you can only have a toy app.这似乎是最大的障碍之一 - 因为没有导航,您只能拥有一个玩具应用程序。 Anyone aware of the roadmap here?有人知道这里的路线图吗?

"

New Jetpack lib has published for Compose navigation.为 Compose 导航发布了新的 Jetpack 库。 It is still in alpha.它仍处于 alpha 阶段。

In this new library, now user can able to navigation between different composables with navigation components features.在这个新库中,现在用户可以使用导航组件功能在不同的可组合物之间导航。

Using navigation-compose :使用导航撰写

dependencies {
    def nav_compose_version = "1.0.0-alpha01"
    implementation "androidx.navigation:navigation-compose:$nav_compose_version"
}

Example:例子:

Step 1: create a NavController by using the rememberNavController() method in your composable: Link :第 1 步:通过在可组合中使用rememberNavController()方法创建一个NavController链接

val navController = rememberNavController()

Step 2: Creating the NavHost requires the NavController previously created via rememberNavController() and the route of the starting destination of your graph: Link .第2步:创建NavHost需要NavController以前通过创建rememberNavController()和你的图表的起始目的地的路线: 链接

NavHost(navController, startDestination = "profile") {
    composable("profile") { Profile(...) }
    composable("friendslist") { FriendsList(...) }
    ...
}

Step 3: To navigate to a composable use navigate() :第 3 步:要导航到可组合使用navigate()

fun Profile(navController: NavController) {
    ...
    Button(onClick = { navController.navigate("friends") }) {
        Text(text = "Navigate next")
    }
    ...
}

check more https://developer.android.com/jetpack/compose/navigation检查更多https://developer.android.com/jetpack/compose/navigation

Here is an unofficial approach of navigation in Jetpack Compose.这是 Jetpack Compose 中的一种非官方导航方法。 Try it out until you get an official word from the Google android devs.尝试一下,直到您从 Google android 开发人员那里得到官方消息。

compose-router组合路由器

https://github.com/zsoltk/compose-router https://github.com/zsoltk/compose-router

It seems that they are moving away from XML.似乎他们正在远离 XML。

The new official samples released after the release of 1.0.0-alpha, have a shared code to manage backstack and navigation. 1.0.0-alpha 发布后发布的新官方示例,具有管理后台堆栈和导航的共享代码。 This code is not part of the library yet.此代码还不是库的一部分。

https://github.com/android/compose-samples/blob/master/Owl/app/src/main/java/com/example/owl/ui/utils/Navigation.kt https://github.com/android/compose-samples/blob/master/Jetsnack/app/src/main/java/com/example/jetsnack/ui/utils/Navigation.kt https://github.com/android/compose-samples/blob/master/Owl/app/src/main/java/com/example/owl/ui/utils/Navigation.kt https://github.com/android /compose-samples/blob/master/Jetsnack/app/src/main/java/com/example/jetsnack/ui/utils/Navigation.kt

I have writed how to handle Navigation with Jetpack Compose我已经写了如何使用 Jetpack Compose 处理导航

Link : https://medium.com/@gsaillen95/how-to-handle-navigation-in-jetpack-compose-a9ac47f7f975链接: https : //medium.com/@gsaillen95/how-to-handle-navigation-in-jetpack-compose-a9ac47f7f975

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Jetpack Compose + Navigation(无片段)处理后退导航 - How to handle back navigation with Jetpack Compose + Navigation (without fragments) 如何使用 Jetpack Compose Navigation 处理弹出多个屏幕 - How to handle popping back multiple screens with Jetpack Compose Navigation 如何在 Jetpack Compose 中将 AlertDialog 与 Navigation 组件集成? - How to integrate AlertDialog with Navigation component in Jetpack Compose? 如何在 Jetpack Compose Navigation 中正确使用 Viewmodel - How to use Viewmodel properly in Jetpack Compose Navigation 如何在 Jetpack Compose 中保存和恢复导航状态? - How to save and restore navigation state in Jetpack Compose? 如何删除 Jetpack Compose Navigation 中的默认过渡 - How to remove default transitions in Jetpack Compose Navigation 如何处理 Jetpack Compose 中被拒绝的权限 - How to handle permission denied in jetpack compose 如何使用 Jetpack Compose 处理 Activity.onActivityResult()? - How to handle Activity.onActivityResult() with Jetpack Compose? 如何处理 Jetpack Compose 的 TextField 中的 doOnTextChanged 和 doBeforeTextChanged? - How to handle doOnTextChanged and doBeforeTextChanged in TextField of Jetpack Compose? 如何处理 Jetpack Compose 中文本的可见性? - How to handle visibility of a Text in Jetpack Compose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM