In Jetpack Compose, how is navigation supposed to be done? 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. 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? Or is there a brand new navigation concept coming, perhaps similar to SwiftUI (navigationlink, etc)? 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?

推荐答案

新的Jetpack库已发布用于 compose 导航.它还在阿尔法.

在这个新的库中,现在用户可以使用导航组件功能在不同的组件之间导航.

使用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:

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.

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

Step 3:要导航至可组合使用navigate(),请执行以下操作:

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

查看更多https://developer.android.com/jetpack/compose/navigation

Kotlin相关问答推荐

Jetpack Compose中的数字 Select 器问题

外键是主键的一部分,但不是索引的一部分.房间

使用数据存储首选项Kotlin Jetpack Compose

Regex(Kotlin)仅匹配句子末尾的句号,而忽略中间的句号,如缩写

如何创建一个空的kotlin工作?

Gradle:无法创建 ExtensiblePolymorphicDomainObjectContainer

Kotlin .如何用从 1 到 90 的 5 个唯一数字填充列表中的每一行?

使用 StateFlow 时如何移除监听器?

在 Kotlin 中,如何绑定扩展方法以在接收器范围函数中工作

每个 Kotlin 版本的默认 Kotlin 语言版本是什么?

Jetpack compose 可滚动表格

如何在 Kotlin 中使用具有继承的泛型

判断 AAR 元数据值时发现的一个或多个问题:

@InlineOnly 注释是什么意思?

在 Koin 中提供一个 Instance 作为其接口

(kotlin的Moshi)@Json vs@field:Json

如果kotlin已经有了getter和setter,为什么在数据类中有componentN函数?

Kotlin:在何时使用枚举

如何在 firebase 数据库中使用 kotlin 协程

具有多个 parameter的 Kotlin 枚举