'Programmatically Navigate in Navigation Drawer set up with Navigation Component

Can anyone help me out with this?

I currently have a Navigation Drawer that is setup with Navigation Component in my MainActivity

Code:

   val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        navController = navHostFragment.navController
        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.fragment1,
                R.id.fragment2,
                R.id.fragment3
            
            ), layoutDrawer
        )

        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)
        navView.setupWithNavController(navController)

What I want:

I want to programmatically navigate to a specific fragment without the use of R.id.action since I will be writing the code in the MainActivity which is not part of my NavGraph

Something like

navController.navigate(R.id.fragment3) 

would be nice



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source