NavHostFragment

public class NavHostFragment implements NavHost

Known direct subclasses
DynamicNavHostFragment

The NavHostFragment for dynamic features.


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.:

<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_sample"
app:defaultNavHost="true" />
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"/>;
</androidx.drawerlayout.widget.DrawerLayout>

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 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 fields

final boolean
final @NonNull Fragment.AnimationInfo
final @NonNull Bundle
final int
final boolean
final @NonNull FragmentManager
final @NonNull ViewGroup
final int
final @NonNull ViewModelProvider.Factory
final boolean
final boolean
final int
final @NonNull FragmentManager
final boolean
final boolean
final boolean
final boolean
final @NonNull FragmentHostCallback<@NonNull ?>
final boolean
final boolean
final @NonNull LayoutInflater
final @NonNull LifecycleRegistry
final @NonNull Lifecycle.State
final boolean
final @NonNull Fragment
final boolean
final @NonNull Runnable
final @RestrictTo(value = [RestrictTo.Scope.LIBRARY]) @Nullable String
final boolean
final boolean
final boolean
final boolean
final @NonNull Bundle
final @NonNull SavedStateRegistryController
final @Nullable Boolean
final @NonNull Bundle
final @NonNull SparseArray<@NonNull Parcelable>
final int
final @NonNull String
final @NonNull Fragment
final int
final @NonNull String
final boolean
final @NonNull View
final @Nullable FragmentViewLifecycleOwner
final @NonNull MutableLiveData<@NonNull LifecycleOwner>
final @NonNull String
final @NonNull NavController

The navigation controller for this navigation host.

Public constructors

Public methods

@CallSuper void
@CallSuper void
onCreate(@Nullable Bundle savedInstanceState)
@Nullable View
onCreateView(
    @NonNull LayoutInflater inflater,
    @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState
)
void
@CallSuper void
onInflate(
    @NonNull Context context,
    @NonNull AttributeSet attrs,
    @Nullable Bundle savedInstanceState
)
@CallSuper void
onPrimaryNavigationFragmentChanged(
    boolean isPrimaryNavigationFragment
)
@CallSuper void
void
onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)

Inherited methods

From class Fragment
void
callStartTransitionListener(boolean calledDirectly)
@NonNull FragmentContainer
void
dump(
    @NonNull String prefix,
    @Nullable FileDescriptor fd,
    @NonNull PrintWriter writer,
    @Nullable Array<@NonNull String> args
)
final boolean
@Nullable Fragment
@NonNull String
final @Nullable FragmentActivity
boolean
boolean
@NonNull View
final @Nullable Bundle
final @NonNull FragmentManager
@Nullable Context
@NonNull ViewModelProvider.Factory
int
@Nullable Object
@NonNull SharedElementCallback
int
@Nullable Object
@NonNull SharedElementCallback
@NonNull View
final @Nullable FragmentManager

This method is deprecated. Deprecated in Java

final @Nullable Object
final int
final @NonNull LayoutInflater
@NonNull @RestrictTo(value = [RestrictTo.Scope.LIBRARY_GROUP_PREFIX]) LayoutInflater
getLayoutInflater(@Nullable Bundle savedFragmentState)

This method is deprecated. Deprecated in Java

@NonNull Lifecycle
@NonNull LoaderManager

This method is deprecated. Deprecated in Java

int
final @Nullable Fragment
final @NonNull FragmentManager
boolean
int
int
float
@Nullable Object
final @NonNull Resources
final boolean

This method is deprecated. Deprecated in Java

@Nullable Object
final @NonNull SavedStateRegistry
@Nullable Object
@Nullable Object
@NonNull ArrayList<@NonNull String>
@NonNull ArrayList<@NonNull String>
final @NonNull String
getString(@StringRes int resId)
final @NonNull String
getString(@StringRes int resId, @Nullable Object formatArgs)
final @Nullable String
final @Nullable Fragment

This method is deprecated. Deprecated in Java

final int

This method is deprecated. Deprecated in Java

final @NonNull CharSequence
getText(@StringRes int resId)
boolean

This method is deprecated. Deprecated in Java

@Nullable View
@MainThread @NonNull LifecycleOwner
@NonNull LiveData<@NonNull LifecycleOwner>
@NonNull ViewModelStore
final @RestrictTo(value = [RestrictTo.Scope.LIBRARY_GROUP_PREFIX]) boolean
final int
void
final boolean
final boolean
final boolean
final boolean
final boolean
final @RestrictTo(value = [RestrictTo.Scope.LIBRARY_GROUP_PREFIX]) boolean
boolean
final boolean
final boolean
final boolean
final boolean
void
@MainThread @CallSuper void
onActivityCreated(@Nullable Bundle savedInstanceState)

This method is deprecated. Deprecated in Java

void
onActivityResult(int requestCode, int resultCode, @Nullable Intent data)

This method is deprecated. Deprecated in Java

@MainThread @CallSuper void

This method is deprecated. Deprecated in Java

@MainThread void

This method is deprecated. Deprecated in Java

@CallSuper void
@MainThread boolean
@MainThread @Nullable Animation
onCreateAnimation(int transit, boolean enter, int nextAnim)
@MainThread @Nullable Animator
onCreateAnimator(int transit, boolean enter, int nextAnim)
@MainThread void
onCreateContextMenu(
    @NonNull ContextMenu menu,
    @NonNull View v,
    @Nullable ContextMenu.ContextMenuInfo menuInfo
)
@MainThread void
@MainThread @CallSuper void
@MainThread void
@MainThread @CallSuper void
@NonNull LayoutInflater
onGetLayoutInflater(@Nullable Bundle savedInstanceState)
@MainThread void
onHiddenChanged(boolean hidden)
@UiThread @CallSuper void
onInflate(
    @NonNull Activity activity,
    @NonNull AttributeSet attrs,
    @Nullable Bundle savedInstanceState
)

This method is deprecated. Deprecated in Java

@MainThread @CallSuper void
void
onMultiWindowModeChanged(boolean isInMultiWindowMode)
@MainThread boolean
@MainThread void
@MainThread @CallSuper void
void
onPictureInPictureModeChanged(boolean isInPictureInPictureMode)
@MainThread void
void
onRequestPermissionsResult(
    int requestCode,
    @NonNull Array<@NonNull String> permissions,
    @NonNull int[] grantResults
)

This method is deprecated. Deprecated in Java

@MainThread @CallSuper void
@MainThread @CallSuper void
@MainThread @CallSuper void
@MainThread @CallSuper void
onViewStateRestored(@Nullable Bundle savedInstanceState)
void
performActivityCreated(@NonNull Bundle savedInstanceState)
void
void
boolean
void
performCreate(@NonNull Bundle savedInstanceState)
boolean
performCreateOptionsMenu(
    @NonNull Menu menu,
    @NonNull MenuInflater inflater
)
void
performCreateView(
    @NonNull LayoutInflater inflater,
    @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState
)
void
void
void
@NonNull LayoutInflater
void
void
performMultiWindowModeChanged(boolean isInMultiWindowMode)
boolean
void
void
void
performPictureInPictureModeChanged(boolean isInPictureInPictureMode)
boolean
void
void
void
void
void
void
void
final void
postponeEnterTransition(long duration, @NonNull TimeUnit timeUnit)
final @MainThread @NonNull ActivityResultLauncher<@NonNull I>
<I extends Object, O extends Object> registerForActivityResult(
    @NonNull ActivityResultContract<@NonNull I, @NonNull O> contract,
    @NonNull ActivityResultCallback<@NonNull O> callback
)
final @MainThread @NonNull ActivityResultLauncher<@NonNull I>
<I extends Object, O extends Object> registerForActivityResult(
    @NonNull ActivityResultContract<@NonNull I, @NonNull O> contract,
    @NonNull ActivityResultRegistry registry,
    @NonNull ActivityResultCallback<@NonNull O> callback
)
void
final void
requestPermissions(
    @NonNull Array<@NonNull String> permissions,
    int requestCode
)

This method is deprecated. Deprecated in Java

final @NonNull FragmentActivity
final @NonNull Bundle
final @NonNull Context
final @NonNull FragmentManager

This method is deprecated. Deprecated in Java

final @NonNull Object
final @NonNull Fragment
final @NonNull View
void
final void
restoreViewState(@NonNull Bundle savedInstanceState)
void
void
void
setAnimations(int enter, int exit, int popEnter, int popExit)
void
void
void
void
void
void
void
setHasOptionsMenu(boolean hasMenu)
void
void
setMenuVisibility(boolean menuVisible)
void
setNextTransition(int nextTransition)
void
setPopDirection(boolean isPop)
void
void
void
setRetainInstance(boolean retain)

This method is deprecated. Deprecated in Java

void
void
void
setSharedElementNames(
    @Nullable ArrayList<@NonNull String> sharedElementSourceNames,
    @Nullable ArrayList<@NonNull String> sharedElementTargetNames
)
void
void
setTargetFragment(@Nullable Fragment fragment, int requestCode)

This method is deprecated. Deprecated in Java

void
setUserVisibleHint(boolean isVisibleToUser)

This method is deprecated. Deprecated in Java

boolean
void
void
startActivity(@NonNull Intent intent, @Nullable Bundle options)
void
startActivityForResult(@NonNull Intent intent, int requestCode)

This method is deprecated. Deprecated in Java

void
startActivityForResult(
    @NonNull Intent intent,
    int requestCode,
    @Nullable Bundle options
)

This method is deprecated. Deprecated in Java

void
startIntentSenderForResult(
    @NonNull IntentSender intent,
    int requestCode,
    @Nullable Intent fillInIntent,
    int flagsMask,
    int flagsValues,
    int extraFlags,
    @Nullable Bundle options
)

This method is deprecated. Deprecated in Java

void
@NonNull String
void

Public fields

mAdded

@NonNull
public final boolean mAdded

mAnimationInfo

@NonNull
public final @NonNull Fragment.AnimationInfo mAnimationInfo

mArguments

@NonNull
public final @NonNull Bundle mArguments

mBackStackNesting

@NonNull
public final int mBackStackNesting

mBeingSaved

@NonNull
public final boolean mBeingSaved

mChildFragmentManager

@NonNull
public final @NonNull FragmentManager mChildFragmentManager

mContainer

@NonNull
public final @NonNull ViewGroup mContainer

mContainerId

@NonNull
public final int mContainerId

mDefaultFactory

@NonNull
public final @NonNull ViewModelProvider.Factory mDefaultFactory

mDeferStart

@NonNull
public final boolean mDeferStart

mDetached

@NonNull
public final boolean mDetached

mFragmentId

@NonNull
public final int mFragmentId

mFragmentManager

@NonNull
public final @NonNull FragmentManager mFragmentManager

mFromLayout

@NonNull
public final boolean mFromLayout

mHasMenu

@NonNull
public final boolean mHasMenu

mHidden

@NonNull
public final boolean mHidden

mHiddenChanged

@NonNull
public final boolean mHiddenChanged

mHost

@NonNull
public final @NonNull FragmentHostCallback<@NonNull ?> mHost

mInLayout

@NonNull
public final boolean mInLayout

mIsCreated

@NonNull
public final boolean mIsCreated

mLayoutInflater

@NonNull
public final @NonNull LayoutInflater mLayoutInflater

mLifecycleRegistry

@NonNull
public final @NonNull LifecycleRegistry mLifecycleRegistry

mMaxState

@NonNull
public final @NonNull Lifecycle.State mMaxState
@NonNull
public final boolean mMenuVisible

mParentFragment

@NonNull
public final @NonNull Fragment mParentFragment

mPerformedCreateView

@NonNull
public final boolean mPerformedCreateView

mPostponedDurationRunnable

@NonNull
public final @NonNull Runnable mPostponedDurationRunnable

mPreviousWho

@RestrictTo(value = [RestrictTo.Scope.LIBRARY])
@Nullable
public final @RestrictTo(value = [RestrictTo.Scope.LIBRARY]) @Nullable String mPreviousWho

mRemoving

@NonNull
public final boolean mRemoving

mRestored

@NonNull
public final boolean mRestored

mRetainInstance

@NonNull
public final boolean mRetainInstance

mRetainInstanceChangedWhileDetached

@NonNull
public final boolean mRetainInstanceChangedWhileDetached

mSavedFragmentState

@NonNull
public final @NonNull Bundle mSavedFragmentState

mSavedStateRegistryController

@NonNull
public final @NonNull SavedStateRegistryController mSavedStateRegistryController

mSavedUserVisibleHint

@Nullable
public final @Nullable Boolean mSavedUserVisibleHint

mSavedViewRegistryState

@NonNull
public final @NonNull Bundle mSavedViewRegistryState

mSavedViewState

@NonNull
public final @NonNull SparseArray<@NonNull ParcelablemSavedViewState

mState

@NonNull
public final int mState

mTag

@NonNull
public final @NonNull String mTag

mTarget

@NonNull
public final @NonNull Fragment mTarget

mTargetRequestCode

@NonNull
public final int mTargetRequestCode

mTargetWho

@NonNull
public final @NonNull String mTargetWho

mUserVisibleHint

@NonNull
public final boolean mUserVisibleHint

mView

@NonNull
public final @NonNull View mView

mViewLifecycleOwner

@Nullable
public final @Nullable FragmentViewLifecycleOwner mViewLifecycleOwner

mViewLifecycleOwnerLiveData

@NonNull
public final @NonNull MutableLiveData<@NonNull LifecycleOwnermViewLifecycleOwnerLiveData

mWho

@NonNull
public final @NonNull String mWho
@NonNull
public final @NonNull NavController navController

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

Returns
@NonNull NavController

this host's navigation controller

Throws
kotlin.IllegalStateException

if called before onCreate

Public constructors

@NonNull
public final NavHostFragment()

Public methods

onAttach

@CallSuper
@NonNull
public @CallSuper void onAttach(@NonNull Context context)

onCreate

@CallSuper
@NonNull
public @CallSuper void onCreate(@Nullable Bundle savedInstanceState)

onCreateView

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

onDestroyView

@NonNull
public void onDestroyView()

onInflate

@CallSuper
@NonNull
public @CallSuper void onInflate(
    @NonNull Context context,
    @NonNull AttributeSet attrs,
    @Nullable Bundle savedInstanceState
)

onPrimaryNavigationFragmentChanged

@CallSuper
@NonNull
public @CallSuper void onPrimaryNavigationFragmentChanged(
    boolean isPrimaryNavigationFragment
)

onSaveInstanceState

@CallSuper
@NonNull
public @CallSuper void onSaveInstanceState(@NonNull Bundle outState)

onViewCreated

@NonNull
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)