NavController旋转后无当前导航节点

问题描述 投票:3回答:1

我正在制作应用程序,它支持不同的设备方向。导航由Android Jetpack的Navigation进行。下面是用于横向显示的应用主屏幕。它是列表包装器片段(它是NavHostFragment,在fragment标签中添加到活动的布局中),包装器包括列表片段(fragment)和详细信息片段(FrameLayout)。纵向方向类似(包装器和列表在其中,详细信息可通过抛出导航访问)。

App main screen

我的问题是我更改设备方向后出现异常

java.lang.IllegalStateException:没有当前的导航节点

具有模拟数据的布局的第一个版本运行良好,在将ROOM添加到我的应用,新订单和更新订单片段后,出现错误。可惜,我无法更准确地定位错误源。

列表包装代码

class OrderListWrapperFragment : RxFragment() {

    private val disposable = CompositeDisposable()

    var selectedOrderId: Long = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val bundle = savedInstanceState ?: arguments
        bundle?.let {
            selectedOrderId = it.getLong(EXTRA_ORDER_ID)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? = inflater.inflate(R.layout.orders__list_wrapper, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initializeToolbar(toolbar, getString(R.string.orders_title), false)

        newOrderButton
            .clicks()
            .subscribe {
                findNavController()
                    .navigate(R.id.action_orderListWrapperFragment_to_orderNewFragment)
            }
            .addTo(disposable)

        childFragmentManager.registerFragmentLifecycleCallbacks(callback, false)
    }

    override fun onDestroyView() {
        super.onDestroyView()

        childFragmentManager.unregisterFragmentLifecycleCallbacks(callback)

        disposable.clear()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)

        outState.putLong(EXTRA_ORDER_ID, selectedOrderId)
    }

    private val callback = object : FragmentManager.FragmentLifecycleCallbacks() {

        private val disposable = CompositeDisposable()

        override fun onFragmentResumed(fm: FragmentManager, fragment: Fragment) {
            super.onFragmentResumed(fm, fragment)

            if (fragment is OrderListFragment) {
                fragment
                    .selectedItemIdChanges
                    .subscribeBy(onNext = {
                        selectedOrderId = it
                        if (orderDetailsContainer != null) {
                            childFragmentManager.commit {
                                replace(
                                    R.id.orderDetailsContainer,
                                    OrderDetailsFragment.newInstance(it)
                                )
                            }
                        } else {
                            findNavController()
                                .navigate(
                                    R.id.action_orderListWrapperFragment_to_orderDetailsFragment,
                                    bundleOf(EXTRA_ORDER_ID to it)
                                )
                            selectedOrderId = 0
                        }
                    },
                        onError = {
                            Log.d("detailView", it.toString())
                        })
                    .addTo(disposable)

                val orderId = selectedOrderId
                if (orderId != 0L) {
                    if (orderDetailsContainer != null) {
                        childFragmentManager.commit {
                            replace(
                                R.id.orderDetailsContainer,
                                OrderDetailsFragment.newInstance(orderId)
                            )
                        }
                    } else {
                        findNavController()
                            .navigate(//exception throws here
                                R.id.action_orderListWrapperFragment_to_orderDetailsFragment,
                                bundleOf(EXTRA_ORDER_ID to orderId)
                            )
                        selectedOrderId = 0
                    }
                }
            }
        }

        override fun onFragmentPaused(fm: FragmentManager, fragment: Fragment) {
            super.onFragmentPaused(fm, fragment)

            if (fragment is OrderListFragment) {
                disposable.clear()
            }
        }
    }

    companion object {
        private const val EXTRA_ORDER_ID = "EXTRA_ORDER_ID"
    }
}

我的导航图

<?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/navigation_graph"
    app:startDestination="@id/orderListWrapperFragment">

    <fragment
        android:id="@+id/orderListWrapperFragment"
        android:name="com.mtgshipping.app.orders.orderList.OrderListWrapperFragment"
        android:label="OrderListWrapperFragment"
        tools:layout="@layout/orders__list_wrapper">
        <action
            android:id="@+id/action_orderListWrapperFragment_to_orderDetailsFragment"
            app:destination="@id/orderDetailsFragment"/>
        <action
            android:id="@+id/action_orderListWrapperFragment_to_orderNewFragment"
            app:destination="@id/orderNewFragment"/>
        <action
            android:id="@+id/action_orderListWrapperFragment_to_orderUpdateFragment"
            app:destination="@id/orderUpdateFragment"/>
    </fragment>

    <fragment
        android:id="@+id/orderDetailsFragment"
        android:name="com.mtgshipping.app.orders.orderDetails.OrderDetailsFragment"
        android:label="OrderDetailsFragment"
        tools:layout="@layout/orders__order_details">
        <action
            android:id="@+id/action_orderDetailsFragment_to_orderUpdateFragment"
            app:destination="@id/orderUpdateFragment"/>
    </fragment>

    <fragment
        android:id="@+id/orderNewFragment"
        android:name="com.mtgshipping.app.orders.orderNew.OrderNewFragment"
        android:label="OrderNewFragment"
        tools:layout="@layout/orders__order_new">
        <action
            android:id="@+id/action_orderNewFragment_to_orderListWrapperFragment"
            app:destination="@id/orderListWrapperFragment"/>
    </fragment>

    <fragment
        android:id="@+id/orderUpdateFragment"
        android:name="com.mtgshipping.app.orders.orderUpdate.OrderUpdateFragment"
        android:label="OrderUpdateFragment"
        tools:layout="@layout/orders__order_update">
        <action
            android:id="@+id/action_orderUpdateFragment_to_orderListWrapperFragment"
            app:destination="@id/orderListWrapperFragment"/>
    </fragment>
</navigation>

[我在NavController中进行了一些调试,在设备旋转NavDestination currentNode = mBackStack.isEmpty() ? mGraph : mBackStack.getLast().getDestination();mGraph之后,它在第746 null行中显示,其他私有字段也为null。可能是某些因素阻止了NavController正确初始化。如果需要的话,我可以提供布局和其他代码。

android android-fragments kotlin android-architecture-components
1个回答
2
投票

感谢斯拉夫的评论,他是对的。我在应用程序的模块navigation_version = '2.2.0'中将导航模块更新为2.2.0 build.gradle

implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"

此问题不再出现后,似乎是导航错误。

© www.soinside.com 2019 - 2024. All rights reserved.