我正在使用导航组件。在导航时,如果在后退堆栈中已存在该片段,则不希望创建其新实例,并弹出前面已存在的那个。
findNavController().navigate(RequestTransferFragmentDirections.actionRequestTransferFragmentToBlankFragment())
期待找到解决方案。
谢谢。
我正在使用导航组件。在导航时,如果在后退堆栈中已存在该片段,则不希望创建其新实例,并弹出前面已存在的那个。
findNavController().navigate(RequestTransferFragmentDirections.actionRequestTransferFragmentToBlankFragment())
期待找到解决方案。
谢谢。
我也遇到了同样的问题,但之前的解决方案对我不起作用,尽管它们应该可以解决这个问题。顺便说一句,谢谢!:)
这个方法对我有用,适应于你的代码:
findNavController().navigate(
RequestTransferFragmentDirections.actionRequestTransferFragmentToBlankFragment()),
NavOptions.Builder().setLaunchSingleTop(true).build()
)
我在navigate()文档中看到,我们可以通过传递选项,因此通过传递NavOptions.Builder().setLaunchSingleTop(true).build()
将创建这个片段的单个实例。
我在这个模式中实现了仅一种片段类型的请求:
在navigation_graph.xml中,我声明了一个弹出操作指向片段目标。
<action
android:id="@+id/home_action"
app:destination="@id/my_dest"
app:popUpTo="@id/my_dest"
app:popUpToInclusive="true" />
<fragment
android:id="@+id/my_dest"
android:name="com.project.android.fragments.MyFragment"
android:label=""
tools:layout="@layout/my_fragment_layout" />
在代码中我调用了该操作
navController.navigate(R.id.home_action)
我在这里回答,因为我也有同样的问题。以下是对我有效的解决方案。最终,我使用了导航控制器,并在存在后退栈目标时将其弹出,如果不存在,则正常导航到它。
具体实现如下:
if ( ! nav.popBackStack(R.id.action_profile, false)) {
nav.navigate(R.id.action_profile)
}
nav.popBackStack(R.id.action_profile, false)
将返回false;否则,它将弹出到该目标并返回true。布尔值用于同时弹出目标碎片。/**
* Attempts to pop the controller's back stack back to a specific destination.
*
* @param destinationId The topmost destination to retain
* @param inclusive Whether the given destination should also be popped.
*
* @return true if the stack was popped at least once and the user has been navigated to
* another destination, false otherwise
*/
public boolean popBackStack(@IdRes int destinationId, boolean inclusive) {
boolean popped = popBackStackInternal(destinationId, inclusive);
// Only return true if the pop succeeded and we've dispatched
// the change to a new destination
return popped && dispatchOnDestinationChanged();
}
bottomNavigation.setupWithNavController(navController)
bottomNavigation.setOnNavigationItemSelectedListener {
if (it.itemId == R.id.navigation_home) {
navController.popBackStack(R.id.navigation_home, false)
true
}
else
NavigationUI.onNavDestinationSelected(it , navController)
}
上述代码中,每当用户点击底部导航视图中的“主页”(R.id.navigation_home)项时,应用程序将使用popBackStack()返回到现有实例的“主页”目标。