You have to override NavHostFragment's createFragmentNavigator
method and return YourFragmentNavigator
.
YourFragmentNavigator
must override FragmentNavigator's navigate
method.
Copy and paste FragmentNavigator's navigate
method to your YourFragmentNavigator
.
In navigate method, change the line ft.replace(mContainerId, frag);
with
if (fragmentManager.fragments.size <= 0) {
ft.replace(containerId, frag)
} else {
ft.hide(fragmentManager.fragments[fragmentManager.fragments.size - 1])
ft.add(containerId, frag)
}
The solution will look like this:
class YourNavHostFragment : NavHostFragment() {
override fun createFragmentNavigator(): Navigator<...> {
return YourFragmentNavigator(...)
}}
....
class YourFragmentNavigator(...) : FragmentNavigator(...) {
override fun navigate(...){
....
if (fragmentManager.fragments.size <= 0) {
ft.replace(containerId, frag)
} else {
ft.hide(fragmentManager.fragments[fragmentManager.fragments.size - 1])
ft.add(containerId, frag)
}
....
}}
in your xml use YourNavHostFragment
.
androidx.navigation.fragment.FragmentNavigator#navigate
method you will see that it internally usesft.replace(mContainerId, frag);
. I think the only option here is to start a new activity as a destination. – Kyrylo ZapylaievWebView
is always reloaded when coming back from differentFragment
! And I don't see any way to prevent it. – xinaiz