public static class Navigation


Entry point for navigation operations.

This class provides utilities for finding a relevant NavController instance from various common places in your application, or for performing navigation in response to UI events.

Summary

Public fields

static @NonNull Navigation

Public methods

static final @NonNull View.OnClickListener

Create an android.view.View.OnClickListener for navigating to a destination via a generated NavDirections.

static final @NonNull View.OnClickListener

Create an android.view.View.OnClickListener for navigating to a destination.

static final @NonNull NavController

Find a NavController given a local View.

static final @NonNull NavController
findNavController(@NonNull Activity activity, @IdRes int viewId)

Find a NavController given the id of a View and its containing Activity.

static final void

Associates a NavController with the given View, allowing developers to use findNavController and findNavController with that View or any of its children to retrieve the NavController.

Public fields

INSTANCE

Added in 2.4.0
public static @NonNull Navigation INSTANCE

Public methods

createNavigateOnClickListener

Added in 2.1.0
public static final @NonNull View.OnClickListener createNavigateOnClickListener(@NonNull NavDirections directions)

Create an android.view.View.OnClickListener for navigating to a destination via a generated NavDirections.

Parameters
@NonNull NavDirections directions

directions that describe this navigation operation

Returns
@NonNull View.OnClickListener

a new click listener for setting on an arbitrary view

createNavigateOnClickListener

Added in 1.0.0
public static final @NonNull View.OnClickListener createNavigateOnClickListener(@IdRes int resId, Bundle args)

Create an android.view.View.OnClickListener for navigating to a destination. This supports both navigating via an action and directly navigating to a destination.

Parameters
@IdRes int resId

an action id or a destination id to navigate to when the view is clicked

Bundle args

arguments to pass to the final destination

Returns
@NonNull View.OnClickListener

a new click listener for setting on an arbitrary view

findNavController

Added in 1.0.0
public static final @NonNull NavController findNavController(@NonNull View view)

Find a NavController given a local View.

This method will locate the NavController associated with this view. This is automatically populated for views that are managed by a NavHost and is intended for use by various listener interfaces.

Parameters
@NonNull View view

the view to search from

Returns
@NonNull NavController

the locally scoped NavController to the given view

Throws
kotlin.IllegalStateException

if the given view does not correspond with a NavHost or is not within a NavHost.

findNavController

Added in 1.0.0
public static final @NonNull NavController findNavController(@NonNull Activity activity, @IdRes int viewId)

Find a NavController given the id of a View and its containing Activity. This is a convenience wrapper around findNavController.

This method will locate the NavController associated with this view. This is automatically populated for the id of a NavHost and its children.

Parameters
@NonNull Activity activity

The Activity hosting the view

@IdRes int viewId

The id of the view to search from

Returns
@NonNull NavController

the NavController associated with the view referenced by id

Throws
kotlin.IllegalStateException

if the given viewId does not correspond with a NavHost or is not within a NavHost.

setViewNavController

Added in 1.0.0
public static final void setViewNavController(@NonNull View view, NavController controller)

Associates a NavController with the given View, allowing developers to use findNavController and findNavController with that View or any of its children to retrieve the NavController.

This is generally called for you by the hosting NavHost.

Parameters
@NonNull View view

View that should be associated with the given NavController

NavController controller

The controller you wish to later retrieve via findNavController