I am using Kotlin and have all references added in my project.
// Navigation
implementation "android.arch.navigation:navigation-common-ktx:$rootProject.nav_version"
implementation "android.arch.navigation:navigation-fragment-ktx:$rootProject.nav_version"
implementation "android.arch.navigation:navigation-runtime-ktx:$rootProject.nav_version"
implementation "android.arch.navigation:navigation-ui-ktx:$rootProject.nav_version"
I also have these on top of the build.gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'androidx.navigation.safeargs'
And I am using it like this inside my fragment
class HomeFragment : BaseFragment(){
...
override fun onCategoryItemClicked(category: Category) {
view.findNavController()?.navigate(R.id.phrasesFragment)
}
}
I can see this generated extension(file) too
fun Fragment.findNavController(): NavController =
NavHostFragment.findNavController(this)
NavHostFragment.findNavController(fragment)
is applicable for a container fragment containing child fragments. If you want to access theNavController
for a fragment definition in the activity useView.findNavController()
method – Tamera