当使用androidx.fragment.app.FragmentContainerView
作为导航宿主(navHost)而非常规fragment
时,在方向更改后应用程序无法导航到目的地。
我会收到以下错误消息:
java.lang.IllegalStateException: no current navigation node
我是否存在应该知道的陷阱以正确使用它,或者我的导航组件使用方式不正确?
这是一个简单的带有视图的活动XML:
...
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:navGraph="@navigation/nav_simple" />
...
导航代码:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_legislator.xml"
app:startDestination="@id/initialFragment">
<fragment
android:id="@+id/initialFragment"
android:name="com.example.fragmenttag.InitialFragment"
android:label="Initial Fragment"
tools:layout="@layout/initial_fragment">
<action
android:id="@+id/action_initialFragment_to_destinationFragment"
app:destination="@id/destinationFragment" />
</fragment>
<fragment
android:id="@+id/destinationFragment"
android:name="com.example.fragmenttag.DestinationFragment"
android:label="Destination Fragment"
tools:layout="@layout/destination_fragment" />
</navigation>
这里有一个 GitHub 存储库,您可以很容易地复现一个 bug:https://github.com/dmytroKarataiev/navHostBug
<fragment>
放入FragmentContainerView
中。 - EpicPandaForce