当使用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
发布于 2019-11-06 21:08:49
当没有图形集并试图调用no current navigation node时,就会发生navigate()错误。如果只在使用FragmentContainerView时和配置更改之后才会发生这种情况,那么这将与这只虫子相关,后者是固定的,并计划在导航2.2.0-rc03中发布。
要解决此问题,可以切换回<fragment>或删除app:navGraph="@navigation/nav_simple",然后调用navController.setGraph(R.navigation.nav_simple)。
发布于 2020-01-28 12:16:17
我没有看到任何解释为什么用FragmentContainerView.替换片段视图。这就是为什么要加上这个答案:
在活动中托管片段的常见模式之一是使用FrameLayout。Android社区已经这样做了很多年了,但现在这种情况将会改变。androidx团队引入了一个名为FragmentContainerView的新视图来帮助您。
您所要做的就是用FrameLayout替换您的FragmentContainerView,所有事情都应该是开箱即用的,您不需要在处理片段事务的方式上改变任何东西。
这里获得的好处是改进了对碎片的z排序处理。这里是他们使用的例子,但基本上这意味着,例如,退出和输入两个片段之间的转换不会相互重叠。相反,这个FragmentContainerView将首先启动退出动画,然后启动enter动画。
如果你在问这个能代替标签吗?那么答案是肯定的。您所要做的就是在定义android:name=时添加FragmentContainerView“your_class_name”,它将使用引擎盖下的FragmentTransaction来构建和显示您的片段。这意味着您以后可以使用片段事务来替换它。
发布于 2020-06-22 08:14:15
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.frag_splash, container, false)
}也许你忘了上面第三个“假”参数。只有这三个参数onCreateView乐趣可以接受。
图片来源:https://developer.android.com/reference/androidx/fragment/app/FragmentContainerView
https://stackoverflow.com/questions/58737769
复制相似问题