You have two things you need:
- A reference to the
NavController
.
As per the Navigate to a destination documentation, you can use findNavController(R.id.your_nav_host_fragment)
where R.id.nav_host_fragment
is the android:id
you put on your NavHostFragment
in your Activity's layout.
- An action to go to the edit profile fragment.
For this, Navigation allows you to set up global actions - an action that is available from every destination in your graph. This is the correct way of triggering actions from UI provided by your activity.
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_nav"
app:startDestination="@id/mainFragment">
...
<action android:id="@+id/action_global_editProfileFragment"
app:destination="@id/editProfileFragment"/>
</navigation>
When using Safe Args with a global action, this will generate a MainNavDirections
class that has your action on it.
This means your completed click listener would look like:
headerOfNavDrawer.setOnClickListener{
// Use the Kotlin extension in the -ktx artifacts
val navController = findNavController(R.id.nav_host_fragment)
// Now use the generated Directions class to navigate to the destination
navController.navigate(MainNavDirections.actionGlobalEditProfileFragment())
}