gpt4 book ai didi

android - NavController 在设备旋转后没有当前导航节点

转载 作者:行者123 更新时间:2023-12-02 11:48:38 26 4
gpt4 key购买 nike

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

App main screen

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

java.lang.IllegalStateException: no current navigation node



我的带有模拟数据的布局的第一个版本运行良好,在我将 ROOM 添加到我的应用程序、新订单和更新订单 fragment 后出现错误。很遗憾,我无法更准确地定位错误来源。

列表包装代码
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();设备旋转后 mGraphnull ,其他私有(private)字段也是 null .也许有些东西阻止了 NavController正确初始化。如果需要,我可以提供布局和其他代码。

最佳答案

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

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

做完这个问题就不再出现了,看来是导航的bug。

关于android - NavController 在设备旋转后没有当前导航节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59970818/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com