lightbulb_outline Help shape the future of the Google Play Console, Android Studio, and Firebase. Start survey

NavHostFragment

public class NavHostFragment
extends Fragment implements NavHost

java.lang.Object
   ↳ android.support.v4.app.Fragment
     ↳ androidx.navigation.fragment.NavHostFragment


NavHostFragment provides an area within your layout for self-contained navigation to occur.

NavHostFragment is intended to be used as the content area within a layout resource defining your app's chrome around it, e.g.:

     
         
         
     
 

Each NavHostFragment has a NavController that defines valid navigation within the navigation host. This includes the navigation graph as well as navigation state such as current location and back stack that will be saved and restored along with the NavHostFragment itself.

NavHostFragments register their navigation controller at the root of their view subtree such that any descendant can obtain the controller instance through the Navigation helper class's methods such as Navigation.findNavController(View). View event listener implementations such as View.OnClickListener within navigation destination fragments can use these helpers to navigate based on user interaction without creating a tight coupling to the navigation host.

Summary

Public constructors

NavHostFragment()

Public methods

static NavHostFragment create(int graphResId)

Create a new NavHostFragment instance with an inflated NavGraph resource.

static NavController findNavController(Fragment fragment)

Find a NavController given a local Fragment.

NavController getNavController()

Returns the navigation controller for this navigation host.

void onAttach(Context context)
void onCreate(Bundle savedInstanceState)
View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState)
void onSaveInstanceState(Bundle outState)
void onViewCreated(View view, Bundle savedInstanceState)
void setGraph(int graphResId)

Set a NavGraph for this navigation host's NavController by resource id.

Protected methods

Navigator<? extends FragmentNavigator.Destination> createFragmentNavigator()

Create the FragmentNavigator that this NavHostFragment will use.

Inherited methods

Public constructors

NavHostFragment

public NavHostFragment ()

Public methods

create

public static NavHostFragment create (int graphResId)

Create a new NavHostFragment instance with an inflated NavGraph resource.

Parameters
graphResId int: resource id of the navigation graph to inflate

Returns
NavHostFragment a new NavHostFragment instance

findNavController

public static NavController findNavController (Fragment fragment)

Find a NavController given a local Fragment.

This method will locate the NavController associated with this Fragment, looking first for a NavHostFragment along the given Fragment's parent chain. If a NavController is not found, this method will look for one along this Fragment's view hierarchy as specified by Navigation.findNavController(View).

Parameters
fragment Fragment: the locally scoped Fragment for navigation

Returns
NavController the locally scoped NavController for navigating from this Fragment

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

getNavController

public NavController getNavController ()

Returns the navigation controller for this navigation host. This method will return null until this host fragment's onCreate(Bundle) has been called and it has had an opportunity to restore from a previous instance state.

Returns
NavController this host's navigation controller

Throws
IllegalStateException if called before onCreate(Bundle)

onAttach

public void onAttach (Context context)

Parameters
context Context

onCreate

public void onCreate (Bundle savedInstanceState)

Parameters
savedInstanceState Bundle

onCreateView

public View onCreateView (LayoutInflater inflater, 
                ViewGroup container, 
                Bundle savedInstanceState)

Parameters
inflater LayoutInflater

container ViewGroup

savedInstanceState Bundle

Returns
View

onInflate

public void onInflate (Context context, 
                AttributeSet attrs, 
                Bundle savedInstanceState)

Parameters
context Context

attrs AttributeSet

savedInstanceState Bundle

onSaveInstanceState

public void onSaveInstanceState (Bundle outState)

Parameters
outState Bundle

onViewCreated

public void onViewCreated (View view, 
                Bundle savedInstanceState)

Parameters
view View

savedInstanceState Bundle

setGraph

public void setGraph (int graphResId)

Set a NavGraph for this navigation host's NavController by resource id. The existing graph will be replaced.

Parameters
graphResId int: resource id of the navigation graph to inflate

Protected methods

createFragmentNavigator

protected Navigator<? extends FragmentNavigator.Destination> createFragmentNavigator ()

Create the FragmentNavigator that this NavHostFragment will use. By default, this uses FragmentNavigator, which replaces the entire contents of the NavHostFragment.

This is only called once in onCreate(Bundle) and should not be called directly by subclasses.

Returns
Navigator<? extends FragmentNavigator.Destination> a new instance of a FragmentNavigator