我怎样才能获得由 NavHostFragment(导航架构组件)加载的片段(SupportMapFragment)的引用

How can i get a reference of a Fragment (SupportMapFragment) loaded by the NavHostFragment (of the Navigation Architecture components)

我正在试用 google IO 2018 会议中介绍的导航架构组件。 我正在尝试制作一个包含三个选项卡的应用程序,每个选项卡一个片段。 在其中一个中,我想使用 Google 地图 API 放置一张地图。 通常,您应该在 Activity

的 OnCreate 中执行此操作
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
        .findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

R.id.map isSupportMapFragment里面的idmain_activity.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />

但是,导航架构组件设置略有不同。 main_activity.xml 仅包含一个 NavHostFragment(加载每个屏幕的片段)和一个 BottomNavigationView 以在应用程序的不同目的地之间移动。

<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<fragment
    android:id="@+id/my_nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:navGraph="@navigation/nav_graph"
    app:defaultNavHost="true"
    android:layout_height="match_parent"
    android:layout_width="match_parent" >

</fragment>


<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@android:color/white"
    android:layout_alignParentBottom="true"
    app:itemTextColor="@android:color/white"
    app:menu="@menu/bottom_navigation_main" />

</RelativeLayout>

我的导航图是这样的

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@id/map">

<fragment
    android:id="@+id/endFragment"
    android:name="douglas.leone.easytaxi.EndFragment"
    android:label="fragment_end"
    tools:layout="@layout/fragment_end" />

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" />

<fragment
    android:id="@+id/startFragment"
    android:name="douglas.leone.easytaxi.StartFragment"
    android:label="fragment_start"
    tools:layout="@layout/fragment_start" />

</navigation>

如您所见,第一个和第三个选项卡有两个片段 startFragmentendFragment。在第二个中,我试图引用 SupportMapFragment。 我不能使用标准的 getSupportFragmentManager().findFragmentById(int id) 方式,因为 main_activity.xml 只包含一个 NavHostFragment

我尝试使用 getChildFragmentManager() 进行试验,但没有成功。 我也试过 navController.getCurrentDestination() 但它 returns 一个 NavDestination object 我不能用它来检索加载的片段。

我找到的唯一解决方案是直接在 activity_main.xml 中添加 SupportMapFragment 使其与导航图完全分开,但它更像是一种变通方法而不是解决方案,因为我必须手动处理当用户在另一个屏幕时显示和隐藏地图。

如果有人知道合适的解决方案,请分享。

不要将 SupportMapFragment 直接添加到您的导航图中,您应该添加自己的 Fragment class 来拥有和管理 SupportMapFragment - 它将是要调用的getMapAsync 并且 SupportMapFragment 有自己的布局。

导航的目标是将 Activity 与各个目的地的内容分离。让 Activity 管理单个目的地不是推荐的模式。