我的活动具有底部导航,我想在活动中获取导航片段之一的实例。
我试图通过先获取com.myapp.ui.MapFragment
的实例,然后获取{的实例,来获取活动onCreate
内nav_host_fragment
的实例(在setContentView之后)。 {1}}使用com.myapp.ui.MapFragment
:
childFragmentManager
它找到navFragment的实例,但mapFragment为null。
这是活动布局:
val navFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
val mapFragment = navFragment!!.childFragmentManager.findFragmentById(R.id.navigation_map)
这是导航布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/twitter_activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation"
android:layout_weight="90"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_weight="10"
app:menu="@menu/bottom_nav_menu" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这是活动的<?xml version="1.0" encoding="utf-8"?>
<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"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/navigation_map">
<fragment
android:id="@+id/navigation_map"
android:name="com.myapp.ui.map.MapFragment"
android:label="@string/title_map"
tools:layout="@layout/fragment_map" />
<fragment
android:id="@+id/navigation_places"
android:name="com.myapp.ui.places.PlacesContainerFragment"
android:label="@string/title_places"
tools:layout="@layout/fragment_places_container" />
<fragment
android:id="@+id/navigation_profile"
android:name="com.myapp.ui.profile.ProfileFragment"
android:label="@string/title_profile"
tools:layout="@layout/fragment_profile" />
</navigation>
:
onCreate
答案 0 :(得分:0)
根据FragmentNavigator
documentation(在FragmentTransaction
中执行NavController
的基础API):
可以通过调用FragmentManager.getPrimaryNavigationFragment()并将传递给此FragmentNavigator的FragmentManager调用来检索FragmentNavigator角度的当前Fragment。
val navFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
val mapFragment = navFragment!!.childFragmentManager.getPrimaryNavigationFragment()