2

When creating a fragment, I download data from the network. How to avoid reloading when clicking on an already active menu item? Transitions work with Navigation components. I know that I need to listen for a press, but I have not figured out the implementation.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navController = findNavController(R.id.fragmentContainerView)

        val appBarConfiguration = AppBarConfiguration(setOf(R.id.ruleListFragment))
        setupActionBarWithNavController(navController, appBarConfiguration)

        bottomNavigatinView.setupWithNavController(navController)
        
        bottomNavigatinView.setOnNavigationItemSelectedListener { 
            //TODO
        }
    }
}
ADM
  • 20,406
  • 11
  • 52
  • 83
pie
  • 127
  • 9

2 Answers2

5

If you only want to prevent fragments from recreating when client press already active menu item this is the only code you need:

bottomNavigatinView.setOnNavigationItemReselectedListener { 
    // Empty Block -> Do not write any code here
}

Saman Sattari
  • 3,322
  • 6
  • 30
  • 46
1

You can use this code to prevent reselecting the current item

 bottomNav.setOnNavigationItemReselectedListener {

    }

you can do whatever you want but I kept it empty to prevent reselect the currently active item.

mmdreza baqalpour
  • 1,106
  • 9
  • 18