Search code examples
androidbottomnavigationview

BottomNavigationView doubleclick


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
        }
    }
}

Solution

  • 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
    }