本文介绍了&lt;androidx.fragment.app.FragmentContainerView&gt;vs <片段>作为 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: 没有当前导航节点

是否有我应该知道的正确使用它的问题,或者我使用导航组件的方式不正确?

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

推荐答案

no current navigation node 错误发生在没有图形集并且您尝试调用 navigate().如果它仅在您使用 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.

要解决此问题,您可以切换回 或删除 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;vs <片段>作为 NavHost 的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:42