问题描述
我正在使用导航组件.导航时我不想创建新的片段实例,如果它已经存在于 backstack 中并弹出前面已经存在的片段.
I am using Navigation component. when navigating I want to not create new instance of fragment if it already exists in backstack and pop that already existing in front.
findNavController().navigate(RequestTransferFragmentDirections.actionRequestTransferFragmentToBlankFragment())
寻找解决方案.
谢谢.
推荐答案
我遇到了同样的问题,但不幸的是,以前的解决方案对我不起作用,尽管它们应该解决问题.谢谢顺便说一句!:)
I faced the same problem, but the previous solutions didn't work for me unfortunately, in spite of they are supposed to fix the issue. Thanks btw! :)
这对我有用,适合您的代码是:
This worked for me, adapted to your code would be:
findNavController().navigate(
RequestTransferFragmentDirections.actionRequestTransferFragmentToBlankFragment()),
NavOptions.Builder().setLaunchSingleTop(true).build()
)
我在navigate() 文档,我们可以通过 options,因此通过传递 NavOptions.Builder().setLaunchSingleTop(true).build()
将创建此类片段的单个实例.
I saw in the navigate() documentation that we can pass options, so by passing NavOptions.Builder().setLaunchSingleTop(true).build()
will create a single instance of such fragment.
这篇关于导航到目的地时仅创建一个片段实例(android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!