android - NavController 在设备旋转后没有当前导航节点
问题描述
我正在制作应用程序,它支持不同的设备方向。导航由 Android Jetpack 的 Navigation 进行。横向应用程序主屏幕如下所示。它是列表包装片段(它是NavHostFragment
,它被添加到fragment
标签中的活动布局中),包装包括列表片段(fragment
)和细节片段(FrameLayout
)。纵向是相似的(其中的包装器和列表,可以通过导航访问详细信息)。
我的问题是更改设备方向后出现异常
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
显示在第746行,其他私有字段也是。也许有些东西阻止了正确初始化。如果需要,我可以提供布局和其他代码。NavDestination currentNode = mBackStack.isEmpty() ? mGraph : mBackStack.getLast().getDestination();
mGraph
null
null
NavController
解决方案
感谢斯拉夫的评论,他是对的。navigation_version = '2.2.0'
我在应用程序的模块中将导航模块更新为 2.2.0build.gradle
implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
做完这个问题就不再出现了,看来是导航的bug。
推荐阅读
- c# - 对于每个条目具有多个值的选择列表,如何处理空条目?
- angular - ExpressionChangedAfterItHasBeenCheckedError:将加载器组件与加载器拦截器服务一起使用时
- docker - Docker 没有运行 dist 文件夹
- jenkins - 如何使用 Jenkins 声明式管道状态在 Jenkins 中发布 Jacoco 报告
- c# - Dotnet Core - 无法添加对具有基类的另一个项目的引用
- symfony - 替换 Symfony 容器中的私有服务进行测试
- mysql - 用于计算每天结束时的活跃用户数的 SQL 查询
- python - 创建空 DataFrame:如果指定了索引,则 dtype 会更改
- java - 如何从 Java 类打开用 Kotlin 类编写的 DialogFragment
- php - Laravel 错误:缺少必需的参数