Setting NavGraph programmatically
I found a solution:
//Setup the navGraph for this activity
val myNavHostFragment: NavHostFragment = my_nav_host_fragment as NavHostFragment
val inflater = myNavHostFragment.navController.navInflater
val graph = inflater.inflate(R.navigation.my_nav_graph)
myNavHostFragment.navController.graph = graph
Based on: Navigation Architecture Component- Passing argument data to the startDestination
You can write in your Activity as follows.
findNavController(R.id.my_nav_host_fragment).setGraph(R.navigation.my_nav_graph)
Both of the answers caused exception when i used with DynamicNavHostFragment, so i used
val navHostFragment: NavHostFragment = supportFragmentManager
.findFragmentById(R.id. my_nav_host_fragment) as NavHostFragment
navHostFragment.findNavController().setGraph(R.navigation. my_nav_graph)