android - 从活动中获取嵌套在导航控制器内的片段实例
问题描述
我的活动有底部导航,我想获取活动中导航片段之一的实例。
我试图通过首先获取 的实例然后获取using的实例来获取活动com.myapp.ui.MapFragment
内部的实例(在 setContentView 之后) :onCreate
nav_host_fragment
com.myapp.ui.MapFragment
childFragmentManager
val navFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
val mapFragment = navFragment!!.childFragmentManager.findFragmentById(R.id.navigation_map)
它找到了 navFragment 的实例,但 mapFragment 为空。
这是活动布局:
<?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
:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportActionBar?.hide()
setTheme(preferencesUtils.appTheme)
setContentView(R.layout.activity_main)
val navController = findNavController(R.id.nav_host_fragment)
val appBarConfiguration = AppBarConfiguration(
setOf(R.id.navigation_map, R.id.navigation_places, R.id.navigation_profile)
)
setupActionBarWithNavController(navController, appBarConfiguration)
nav_view.setupWithNavController(navController)
val navFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
val mapFragment = navFragment!!.childFragmentManager.findFragmentById(R.id.navigation_map)
}
解决方案
根据FragmentNavigator
文档FragmentTransaction
(执行s in的底层 API NavController
):
从 FragmentNavigator 的角度来看的当前 Fragment 可以通过调用FragmentManager.getPrimaryNavigationFragment()来检索,并将 FragmentManager 传递给此 FragmentNavigator。
val navFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
val mapFragment = navFragment!!.childFragmentManager.getPrimaryNavigationFragment()
推荐阅读
- rxjs - 仅当 observable 未完成时取消订阅
- javascript - 当值为 100% 时将类添加到 jquery-asProgress.js
- node.js - 请求nodejs获取不可读的数据
- php - Symfony\Component\Translation\TranslatorInterface::setLocale($locale) 的声明必须兼容
- ios - ld:未找到框架 DTXProfiler
- python - Selenium + Python 点击搜索选择但不点击
- debugging - 填写管理员凭据后 Blueprism 中的安装错误
- reactjs - 如何在不触发重新加载的情况下“替换” nextjs 中的路由?
- html - div 中所有 //a[@href] 标记的 XPath,第一个 /li 标记文本为“Something”
- javascript - 在模板中查找键失败