NavHostFragment

public class NavHostFragment extends Fragment implements LifecycleOwner, ViewModelStoreOwner, HasDefaultViewModelProviderFactory, LifecycleOwner, SavedStateRegistryOwner, ActivityResultCaller, NavHost

Object
   ↳ Fragment
     ↳ NavHostFragment
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

Called when a fragment is first attached to its context.

@CallSuper void
onCreate(@Nullable Bundle savedInstanceState)

Called to do initial creation of a fragment.

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

Called to have the fragment instantiate its user interface view.

void

Called when the view previously created by onCreateView has been detached from the fragment.

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

Called when a fragment is being created as part of a view layout inflation, typically from setting the content view of an activity.

@CallSuper void
onPrimaryNavigationFragmentChanged(
    boolean isPrimaryNavigationFragment
)

Callback for when the primary navigation state of this Fragment has changed.

@CallSuper void

Called to ask the fragment to save its current dynamic state, so it can later be reconstructed in a new instance if its process is restarted.

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

Called immediately after onCreateView has returned, but before any saved state has been restored in to the view.

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 @Override boolean
@Nullable Fragment
@NonNull String
final @Nullable FragmentActivity
boolean
boolean
@NonNull View
final @Nullable Bundle
final @NonNull FragmentManager
@Nullable Context
@NonNull @Override ViewModelProvider.Factory

Returns the default androidx.lifecycle.ViewModelProvider.Factory that should be used when no custom {@code Factory} is provided to the androidx.lifecycle.ViewModelProvider constructors.

@AnimRes int
@Nullable Object
@NonNull SharedElementCallback
@AnimRes 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

@Override @NonNull Lifecycle

Returns the Lifecycle of the provider.

@NonNull LoaderManager

This method is deprecated. Deprecated in Java

int
final @Nullable Fragment
final @NonNull FragmentManager
boolean