5

I'm trying to use the new Jetpack navigation component. I use a BottomNavigationView with the navController : NavigationUI.setupWithNavController(bottomNavigation, navController)

But when I'm switching fragments, each fragment will be reloaded! is there any way to stop fragment reloading/refreshment?

Alazar Komar
  • 223
  • 3
  • 12

1 Answers1

3

I had problems with reloading also. This helped me.

override fun onCreate(savedInstanceState: Bundle?) {
...
val navController = Navigation.findNavController(this, R.id.mainFragment) 
bottomNavigation.setOnNavigationItemSelectedListener {
                if (it.itemId != bottomNavigation.selectedItemId)
                    NavigationUI.onNavDestinationSelected(it, navController)
                 true

}

This this stopped reloading on multiple clicks on bottom navigation item.

Chamel
  • 66
  • 4
  • Thanks for the answer this works fine. In my fragment I have a scrollview and when i scroll and leave the fragment a and go to fragment b and when i again return to fragment a instead of loading from where i left it starts reloading again. I rummaged all the internet for an answer but i found none and some are in kotlin which makes hard to convert to java. So, can you please help me with a solution. – CodeRED Innovations Jul 09 '20 at 15:18
  • 2
    This answer only prevent to reload visible fragment. – Viks Feb 09 '21 at 22:39
  • setOnNavigationItemSelectedListener deprecated – Taimoor Khan Jul 02 '22 at 18:11