[英]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.