Alazar Komar
Alazar Komar

Reputation: 233

How to stop fragments reloading/recreation in Jetpack navigation

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?

Upvotes: 5

Views: 1674

Answers (1)

Chamel
Chamel

Reputation: 66

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.

Upvotes: 3

Related Questions