Navigation

public final class Navigation
extends Object

java.lang.Object
   ↳ androidx.navigation.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 methods

static View.OnClickListener createNavigateOnClickListener(int resId)

Create an View.OnClickListener for navigating to a destination.

static View.OnClickListener createNavigateOnClickListener(int resId, Bundle args)

Create an View.OnClickListener for navigating to a destination.

static NavController findNavController(Activity activity, int viewId)

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

static NavController findNavController(View view)

Find a NavController given a local View.

static void setViewNavController(View view, NavController controller)

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

Inherited methods

Public methods

createNavigateOnClickListener

public static View.OnClickListener createNavigateOnClickListener (int resId)

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

Parameters
resId int: an action id or a destination id to navigate to when the view is clicked

Returns
View.OnClickListener a new click listener for setting on an arbitrary view

createNavigateOnClickListener

public static View.OnClickListener createNavigateOnClickListener (int resId, 
                Bundle args)

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

Parameters
resId int: an action id or a destination id to navigate to when the view is clicked

args Bundle: arguments to pass to the final destination

Returns
View.OnClickListener a new click listener for setting on an arbitrary view

findNavController

public static NavController findNavController (Activity activity, 
                int viewId)

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

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

Parameters
activity Activity: The Activity hosting the view

viewId int: The id of the view to search from

Returns
NavController the NavController associated with the view referenced by id

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

findNavController

public static NavController findNavController (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
view View: the view to search from

Returns
NavController the locally scoped NavController to the given view

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

setViewNavController

public static void setViewNavController (View view, 
                NavController controller)

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

This is generally called for you by the hosting NavHost.

Parameters
view View: View that should be associated with the given NavController

controller NavController: The controller you wish to later retrieve via findNavController(View)