本文介绍了活动结果后,此NavController未知的导航目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用导航控制器1.0.0alpha05,并且运行良好,但是当我在活动结果之后执行导航操作时,我正为这个可怕的错误而苦苦挣扎.

i'm using nav controller 1.0.0alpha05 and it is working great, but i'm struggling with this dreaded error when i execute a navigation action after an activity result.

我有一个单一的活动/多个片段结构,特别是一个包含项目列表的片段,另一个具有用于添加新条目的形式的片段.

I have a single activity/multiple fragments structure, in particular a fragment with a list of items and another one with the form for adding a new one.

当我添加另一张没有任何图片的图片时,该图片正在运行,并返回带有项目列表的上一张图片,但是当我拍摄一些照片时,导航期间会出现异常.

When i add another one without any picture it is working and returning to the previous one with the list of items, but when i take some photos i get the exception during the navigation.

错误日志

包含操作的表单片段的导航图:

Navigation graph of the form fragment containing the action:

<fragment
    android:id="@+id/idFormFragment"
    android:name="FormFragment"
    android:label="FormFragment"
    tools:layout="@layout/form_fragment">
    <argument
        android:name="idClient"
        android:defaultValue="-1"
        app:argType="integer" />
    <argument
        android:name="idServer"
        app:argType="string" />
    <action
        android:id="@+id/actionFormToList"
        app:destination="@id/idListFragment" />
</fragment>

带有安全参数的操作调用代码

Code of the action call with safe args

FormFragmentDirections.ActionFormToList action = new FormFragmentDirections.ActionFormToList(sample.getIdJob());
Navigation.findNavController(getView()).navigate(action);

感谢您的时间

推荐答案

好吧,我设法找到了一个荒谬的解决方案...

Well I've managed to find a ridiculous solution...

假设您使用的主机导航片段是 NavHostFragment ,向其添加以下(科特琳)代码:

Assuming that you are using a host navigation fragment that extends from NavHostFragment, add this (Kotlin) code to it:

/*
 * begin DUMB Navigation Component hack
 *
 * This fixes an IllegalArgumentException that can sometimes be thrown from within the
 * Navigation Architecture Component when you try to navigate after the Fragment has had its
 * state restored. It occurs because the navController's currentDestination field is null,
 * which stores where we currently are in the navigation graph. Because it's null, the
 * Navigation Component can't figure out our current position in relation to where we're
 * trying to navigate to, causing the exception to be thrown.
 *
 * This fix gives the navController a little nudge by gently setting it to where we currently
 * are in the navigation graph.
 *
 * This fix is verified as both working AND necessary as of Navigation Components version
 * 1.0.0-alpha07.
 *
 * There's a tiny bit more information at this thread, but it's pretty limited:
 * https://stackoverflow.com/questions/52101617/navigation-destination-unknown-to-this-navcontroller-after-an-activity-result
 */
private var checkCurrentDestination = false

override fun onStart() {
    super.onStart()

    if (checkCurrentDestination && navController.currentDestination == null) {
        navController.navigate(navController.graph.startDestination)
    }

    checkCurrentDestination = false
}

override fun onStop() {
    super.onStop()
    checkCurrentDestination = true
}
/*
 * end DUMB Navigation Component hack
 */

在SEO的努力下,堆栈跟踪将如下所示:

In the efforts of SEO, the stack trace will look like this:

Caused by: java.lang.IllegalArgumentException: navigation destination XX is unknown to this NavController

这篇关于活动结果后,此NavController未知的导航目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:57