buildscript {
ext.kotlin_version = '1.6.0'
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
dependencies {
implementation "androidx.navigation:navigation-fragment-ktx:2.4.0"
}
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph"
app:startDestination="@id/fragment1">
<fragment
android:id="@+id/fragment1"
android:name="com.example.Fragment1"
android:label="Fragment 1">
<action
android:id="@+id/action_fragment1_to_fragment2"
app:destination="@id/fragment2" />
</fragment>
<fragment
android:id="@+id/fragment2"
android:name="com.example.Fragment2"
android:label="Fragment 2">
<action
android:id="@+id/action_fragment2_to_fragment3"
app:destination="@id/fragment3" />
</fragment>
<fragment
android:id="@+id/fragment3"
android:name="com.example.Fragment3"
android:label="Fragment 3">
<action
android:id="@+id/action_fragment3_to_fragment1"
app:destination="@id/fragment1" />
</fragment>
</navigation>
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
NavController navController = navHostFragment.getNavController();
navController.navigate(R.id.action_fragment1_to_fragment2);
int currentDestination = navController.getCurrentDestination().getId();