FragmentNavigator#navigate():
public void navigate(@NonNull Destination destination, @Nullable Bundle args,
@Nullable NavOptions navOptions) {
// ......
ft.replace(mContainerId, frag);
ft.commit();
}
等等。我希望片段使用add。导致重新加载每次导航
答案 0 :(得分:1)
您可以为此提供自定义导航器。
代替FragmentNavigator
,
NavController.getNavigatorProvider().addNavigator().
您的自定义导航器必须带有注释
@Navigator.Name("fragment")
例如
@Navigator.Name("fragment")
HookFragmentNavigator类(私有val委托:FragmentNavigator):
Navigator
override fun navigate(
destination: FragmentNavigator.Destination,
args: Bundle?,
navOptions: NavOptions?,
navigatorExtras: Extras?
): NavDestination? {
"hook delegate navigate".println()
return delegate.navigate(destination, args, navOptions, navigatorExtras)
}
override fun createDestination(): FragmentNavigator.Destination {
return delegate.createDestination()
}
override fun popBackStack(): Boolean {
return delegate.popBackStack()
}
}
findNavController(R.id.fragment).apply {
navigatorProvider.addNavigator(
HookFragmentNavigator(navigatorProvider.getNavigator(FragmentNavigator::class.java))
)
}.navigate(
it,
null,
NavOptions.Builder().setAnimal().build()
)