Вот простой пример того, как можно программно использовать AndroidX Navigation с использованием фрагментов с артефактами KTX:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val container = frameLayout(id = R.id.content) // From Splitties Views DSL. Equivalent to FrameLayout().apply { id = R.id.content }
setContentView(container)
// Add the NavHostFragment if needed
if (savedInstanceState == null) supportFragmentManager.transaction(now = true) {
val fragment = NavHostFragment()
add(R.id.content, fragment)
setPrimaryNavigationFragment(fragment)
}
// Use the Kotlin extension from the -ktx dependencies
// to find the NavController for the given view ID.
val navController = findNavController(R.id.content)
// Create the graph using the Kotlin DSL, setting it on the NavController
navController.graph = navController.createGraph(startDestination = R.id.nav_dest_main) {
fragment<MainFragment>(R.id.nav_dest_main) {
label = TODO("Put an actual CharSequence")
}
fragment<SomeFragment>(R.id.nav_dest_some_fragment) {
label = TODO("Put an actual CharSequence")
}
}
}