Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop fragments reloading/recreation in Jetpack navigation

Tags:

java

android

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?

like image 944
Alazar Komar Avatar asked Oct 16 '22 11:10

Alazar Komar


1 Answers

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.

like image 196
Chamel Avatar answered Oct 18 '22 19:10

Chamel