navigation/fragment/src/main/java/androidx/navigation/fragment/NavHostFragment.java
@NonNull
    public static NavController findNavController(@NonNull Fragment fragment) {
        Fragment findFragment = fragment;
        while (findFragment != null) {
            if (findFragment instanceof NavHostFragment) {
                return ((NavHostFragment) findFragment).getNavController();
            }
            Fragment primaryNavFragment = findFragment.requireFragmentManager()
                    .getPrimaryNavigationFragment();
            if (primaryNavFragment instanceof NavHostFragment) {
                return ((NavHostFragment) primaryNavFragment).getNavController();
            }
            findFragment = findFragment.getParentFragment();
        }

        // Try looking for one associated with the view instead, if applicable
        View view = fragment.getView();
        if (view != null) {
            return Navigation.findNavController(view);
        }
        throw new IllegalStateException("Fragment " + fragment
                + " does not have a NavController set");
    }
/**
     * Find a {@link NavController} given a local {@link Fragment}.
     *
     * <p>This method will locate the {@link NavController} associated with this Fragment,
     * looking first for a {@link NavHostFragment} along the given Fragment's parent chain.
     * If a {@link NavController} is not found, this method will look for one along this
     * Fragment's {@link Fragment#getView() view hierarchy} as specified by
     * {@link Navigation#findNavController(View)}.</p>
     *
     * @param fragment the locally scoped Fragment for navigation
     * @return the locally scoped {@link NavController} for navigating from this {@link Fragment}
     * @throws IllegalStateException if the given Fragment does not correspond with a
     * {@link NavHost} or is not within a NavHost.
     */
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() view hierarchy as specified by
Navigation#findNavController(View).

@param fragment the locally scoped Fragment for navigation
@return 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.
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 () view hierarchy as specified by Navigation#findNavController ( View ) . @param fragment the locally scoped Fragment for navigation @return 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 .
@NonNull
    public static NavController findNavController(@NonNull Fragment fragment) {
        Fragment findFragment = fragment;
        while (findFragment != null) {
            if (findFragment instanceof NavHostFragment) {
                return ((NavHostFragment) findFragment).getNavController();
            }
            Fragment primaryNavFragment = findFragment.requireFragmentManager()
                    .getPrimaryNavigationFragment();
            if (primaryNavFragment instanceof NavHostFragment) {
                return ((NavHostFragment) primaryNavFragment).getNavController();
            }
            findFragment = findFragment.getParentFragment();
        }
        View view = fragment.getView();
        if (view != null) {
            return Navigation.findNavController(view);
        }
        throw new IllegalStateException("Fragment " + fragment
                + " does not have a NavController set");
    }
@ NonNull public static NavController findNavController ( @ NonNull Fragment fragment ) { Fragment findFragment = fragment ; while ( findFragment != null ) { if ( findFragment instanceof NavHostFragment ) { return ( ( NavHostFragment ) findFragment ) . getNavController ( ) ; } Fragment primaryNavFragment = findFragment . requireFragmentManager ( ) . getPrimaryNavigationFragment ( ) ; if ( primaryNavFragment instanceof NavHostFragment ) { return ( ( NavHostFragment ) primaryNavFragment ) . getNavController ( ) ; } findFragment = findFragment . getParentFragment ( ) ; } View view = fragment . getView ( ) ; if ( view != null ) { return Navigation . findNavController ( view ) ; } throw new IllegalStateException ( "Fragment " + fragment + " does not have a NavController set" ) ; }
Find a {@link NavController} given a local {@link Fragment}.
Find a { @link NavController } given a local { @link Fragment } .
// Try looking for one associated with the view instead, if applicable