我有一个简单的导航设置,包括3个屏幕:
Screen 1 -> Screen 2 -> Screen 3
|______________________^
屏幕2是完成一次后可以跳过的注册表单。
我对Jetpack导航图有一个奇怪的问题,从屏幕2-> 3导航时,应用程序由于屏幕1(!)onViewCreated()
被激活而崩溃,并且找不到导航方向:
java.lang.IllegalArgumentException: navigation destination
com.example.app:id/action_screen1_to_screen3 is unknown to this NavController
我的导航图:
<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>
我使用的是自动生成的NavDirections,所以使用错误的资源ID并不是问题。
导航代码:
// 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())
我的Gradle导入:
def nav_version = "2.3.0-alpha04"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
感谢您的帮助,谢谢!
答案 0 :(得分:1)
不确定为什么会发生这种情况,但是我发现的解决方案是在整个屏幕上弹出popUpTo直到整个屏幕:
[...]
app:popUpTo="@id/nav_graph"
app:popUpToInclusive="true"
[...]