简体   繁体   English

Jetpack 导航启动前一屏幕

[英]Jetpack navigation initiating previous screen

I have this simple navigation setup consisting of 3 screens:我有一个由 3 个屏幕组成的简单导航设置:

Screen 1 -> Screen 2 -> Screen 3
    |______________________^

Screen 2 is the signup form that can be skipped when completed once.屏幕 2 是完成一次后可以跳过的注册表单。

I have this weird issue with a Jetpack navigation graph where when navigating from Screen 2 -> 3 the app crashed because of Screen 1 (!) onViewCreated() activated and navigation directions cannot be found:我有一个 Jetpack 导航图的奇怪问题,当从屏幕 2 -> 3 导航时,应用程序崩溃,因为屏幕 1 (!) onViewCreated()激活并且无法找到导航方向:

    java.lang.IllegalArgumentException: navigation destination 
com.example.app:id/action_screen1_to_screen3 is unknown to this NavController

My navigation graph:我的导航图:

   <fragment
        android:id="@+id/screen1"
        android:name="Screen1Fragment"
        android:label="Screen 1"
        tools:layout="@layout/fragment_screen_1">
        <action
            android:id="@+id/action_screen1_to_screen2"
            app:destination="@id/screen2"
            app:launchSingleTop="false"/>
        <action
            android:id="@+id/action_screen1_to_screen3"
            app:destination="@id/screen3"
            app:launchSingleTop="false"
            app:popUpTo="@id/screen1"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/screen2"
        android:name="Screen2Fragment"
        android:label="Screen 2"
        tools:layout="@layout/fragment_screen_2">
        <action
            android:id="@+id/action_screen2_to_screen3"
            app:destination="@id/screen3"
            app:launchSingleTop="false"
            app:popUpTo="@id/screen2"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/screen3"
        android:name="Screen3Fragment"
        android:label="Screen 3"
        tools:layout="@layout/fragment_screen_3">
    </fragment>

I am using auto-generated NavDirections so it's not a matter of using the wrong resource id.我正在使用自动生成的 NavDirections,所以这不是使用错误资源 ID 的问题。

Navigation code:导航代码:

// Screen 1
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        if (isLoggedIn) {
            view?.findNavController()
                ?.navigate(Screen1FragmentDirections.screen1ToSelectScreen3())
        } else {
            signInButton.setOnClickListener {
                val action =
                    Screen1FragmentDirections.screen1ToScreen2()
                view?.findNavController()?.navigate(action)
            }
        }
    }

// Screen 2
view?.findNavController()?.navigate(Screen2FragmentDirections.screen2ToScreen3())

My Gradle imports:我的 Gradle 导入:

    def nav_version = "2.3.0-alpha04"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

Any help is appreciated, thanks!任何帮助表示赞赏,谢谢!

Not sure why it's happening but the solution I found was instead of popUpTo until the current screen, is to popUpTo the entire navigation graph:不知道为什么会这样,但我找到的解决方案是在当前屏幕之前而不是 popUpTo,而是 popUpTo 整个导航图:

[...]
app:popUpTo="@id/nav_graph"
app:popUpToInclusive="true"
[...]

暂无
暂无

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

相关问题 Jetpack 撰写导航关闭应用程序而不是返回上一个屏幕 - Jetpack compose navigation closes app instead of returning to previous screen 在使用 Jetpack Compose 的导航组件时,弹出由可组合表示的屏幕(从“返回堆栈”)返回上一个屏幕? - Popping off screen (from “back stack”) represented by a composable to go back to previous screen while using navigation component of Jetpack Compose? 导航到上一个屏幕 - Navigation for previous screen Jetpack Compose WebView 处理返回导航和 Go 到上一页 - Jetpack Compose WebView Handling Back Navigation And Go To Previous Page 在 Jetpack Compose 中导航到没有先前脚手架的另一个屏幕 - Navigate to another screen without the previous scaffold in Jetpack Compose Jetpack Compose 底部导航、fab 按钮和保存屏幕 state - Jetpack Compose bottom navigation, fab button and save screen state 使用 Jetpack 导航组件在一个屏幕上显示多个片段 - Having multiple fragments at one screen with Jetpack Navigation component 如何防止在jetpack撰写导航中多次创建屏幕 - How to prevent multiple creation of a screen in jetpack compose navigation 如果使用导航组件,如何更新上一个屏幕? - How to Update the previous screen if using navigation component? 如何制作一个深度片段以在屏幕上使用喷气背包导航和BottomNavigationView来占据整个屏幕? - how to make a deep fragment to take the whole screen with a jetpack navigation and BottomNavigationView on screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM