12

我正在使用导航控制器 1.0.0alpha05,它运行良好,但是当我在活动结果后执行导航操作时,我正在努力解决这个可怕的错误。

我有一个单一的活动/多个片段结构,特别是一个带有项目列表的片段和另一个带有添加新项目的表单的片段。

当我添加另一张没有任何图片的图片时,它正在工作并返回带有项目列表的前一张,但是当我拍摄一些照片时,我在导航过程中出现异常。

原因:java.lang.IllegalArgumentException:导航目的地 XX 对此 NavController 是未知的

错误日志

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

<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>

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

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

谢谢你的时间

4

4 回答 4

3

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

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
 */

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
于 2018-11-13T23:28:22.303 回答
2

我找到了一种解决方法,但显然我不能将其视为解决方案:

我认为当片段实例状态恢复时,链接到与此类片段关联的 nav_graph 的操作会以某种方式丢失或其他什么......但我可能是错的

在这种情况下,可以直接使用要导航到的片段的 id,而不是通过安全参数或其 id 指向操作本身。

在这种情况下,如果你想传递参数,你必须通过捆绑包以老式的方式进行。

Bundle args = new Bundle();
args.putString(ID_ARG, arg);
Navigation.findNavController(getView()).navigate(R.id.fragmentId, args);
于 2018-08-31T07:36:23.410 回答
1

就我而言,这是因为我使用fragmentManager?.popBackStack()的是向后导航而不是正确导航,如下所示:

Navigation.findNavController(activity!!, R.id.fragment_container).navigate(
                Navigation.findNavController(activity!!, R.id.fragment_container).graph.startDestination)
于 2019-11-06T23:21:01.370 回答
0

对于我的情况,我通过替换来解决问题 -

 <action
        android:id="@+id/actionFormToList"
        app:destination="@id/idListFragment" />

 <action
            android:id="@+id/actionFormToList"
            app:popUpToInclusive="true" /* If true then also remove the destination from stack while popup */
            app:popUpTo="@id/idFormFragment "  /*The fragment where to land again from destination*/
            app:destination="@id/idListFragment" />
于 2020-11-10T18:37:40.670 回答