< androidx.fragment.app.FragmentContainerView>与< fragment>作为NavHost的视图 [英] <androidx.fragment.app.FragmentContainerView> vs <fragment> as a view for a NavHost

查看:1341
本文介绍了< androidx.fragment.app.FragmentContainerView>与< fragment>作为NavHost的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将androidx.fragment.app.FragmentContainerView用作导航主机而不是常规的fragment应用时,更改方向后将无法导航到目的地.

When using androidx.fragment.app.FragmentContainerView as a navHost instead of a regular fragment app is not able to navigate to a destination after orientation change.

我收到以下错误: java.lang.IllegalStateException: no current navigation node

是否存在我应该正确使用它的陷阱,或者我使用导航组件的方式是否不正确?

Is there a gotcha that I should know about to use it properly or is my way of using nav components is incorrect?

带有视图的简单活动xml:

Simple activity xml with a view:


...
    <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存储库,您可以在其中轻松重现错误: https://github.com/dmytroKarataiev/navHostBug

Here is a github repo where you can easily reproduce a bug: https://github.com/dmytroKarataiev/navHostBug

推荐答案

没有图形集并且您尝试调用navigate()时,会发生no current navigation node错误.如果仅在使用FragmentContainerView时以及更改配置后发生,则这与错误,该错误已修复,并计划与Navigation 2.2.0-rc03一起发布.

The no current navigation node error occurs when there's no graph set and you attempt to call navigate(). If it only occurs when you're using FragmentContainerView and after a configuration change, then this would be related to this bug, which is fixed and scheduled for release with Navigation 2.2.0-rc03.

要变通解决此问题,您可以切换回<fragment>或删除app:navGraph="@navigation/nav_simple",然后调用navController.setGraph(R.navigation.nav_simple).

To work around this issue, you can either switch back to <fragment> or remove app:navGraph="@navigation/nav_simple" and instead call navController.setGraph(R.navigation.nav_simple).

这篇关于&lt; androidx.fragment.app.FragmentContainerView&gt;与&lt; fragment&gt;作为NavHost的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆