NavHostFragment

open class NavHostFragment
kotlin.Any
   ↳ 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 NavGraph 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 android.view.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

Public methods
open Unit
onCreate(savedInstanceState: Bundle?)

open NavController

Returns the NavController for this navigation host.

open Unit
setGraph(graphResId: Int)

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

open Unit
onAttach(context: Context!)

open Unit
onInflate(context: Context!, attrs: AttributeSet!, savedInstanceState: Bundle!)

open Unit

open Unit
onViewCreated(view: View, savedInstanceState: Bundle?)

open View?
onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?)

open static NavController
findNavController(fragment: Fragment)

Find a NavController given a local Fragment.

open static NavHostFragment!
create(graphResId: Int)

Create a new NavHostFragment instance with an inflated NavGraph resource.

Protected methods
open Navigator

Create the FragmentNavigator that this NavHostFragment will use.

Public constructors

<init>

NavHostFragment()

Public methods

onCreate

open fun onCreate(savedInstanceState: Bundle?): Unit

getNavController

open fun getNavController(): NavController

Returns the NavController 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.

Return
NavController: this host's navigation controller
Exceptions
IllegalStateException if called before #onCreate(Bundle)

setGraph

open fun setGraph(graphResId: Int): Unit

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

onAttach

open fun onAttach(context: Context!): Unit

onInflate

open fun onInflate(context: Context!, attrs: AttributeSet!, savedInstanceState: Bundle!): Unit

onSaveInstanceState

open fun onSaveInstanceState(outState: Bundle): Unit

onViewCreated

open fun onViewCreated(view: View, savedInstanceState: Bundle?): Unit

onCreateView

open fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?

findNavController

open static fun findNavController(fragment: Fragment): NavController

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 Fragment#getView() as specified by Navigation#findNavController(View).

Parameters
fragment Fragment: the locally scoped Fragment for navigation
Return
NavController: the locally scoped NavController for navigating from this Fragment
Exceptions
IllegalStateException if the given Fragment does not correspond with a NavHost or is not within a NavHost.

create

open static fun create(graphResId: Int): NavHostFragment!

Create a new NavHostFragment instance with an inflated NavGraph resource.

Parameters
graphResId Int: resource id of the navigation graph to inflate
Return
NavHostFragment!: a new NavHostFragment instance

Protected methods

createFragmentNavigator

protected open fun createFragmentNavigator(): Navigator

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.

Return
Navigator: a new instance of a FragmentNavigator