1

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
  • 18,477
  • 11
  • 47
  • 78
pie
  • 95
  • 6

2 Answers2

4

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
  • 2,845
  • 5
  • 26
  • 38
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.