The NavController
type relies on one or more Navigator
objects to perform the navigation operation. By default, NavController
supports leaving the navigation graph by navigating to another activity using the ActivityNavigator
class and its nested ActivityNavigator.Destination
class.
To navigate to any other type of destination, one or more additional Navigator
objects must be added to the NavController
. For example, when using fragments as destinations, the NavHostFragment
automatically adds the FragmentNavigator
class to its NavController
.
To add a new Navigator
object to a NavController
, use the getNavigatorProvider()
method, followed by the addNavigator()
method.
The following code shows an example of adding a CustomNavigator
object to a NavController
Kotlin
val customNavigator = CustomNavigator() navController.navigatorProvider += customNavigator
Java
CustomNavigator customNavigator = new CustomNavigator(); navController.getNavigatorProvider().addNavigator(customNavigator);
Most Navigator
classes have a nested destination subclass. This subclass can be used to specify additional attributes unique to your destination. For more information about destination subclasses, see the reference documentation for the appropriate Navigator
class.
其他资源
To learn more about navigation, see the following additional resources.