gpt4 book ai didi

android - 从 Activity 中获取嵌套在导航 Controller 内的 fragment 实例

转载 作者:行者123 更新时间:2023-12-02 12:52:41 25 4
gpt4 key购买 nike

我的 Activity 有底部导航,我想获取 Activity 中导航 fragment 之一的实例。

我正在尝试获取 com.myapp.ui.MapFragment 的实例里面onCreate通过首先获取 nav_host_fragment 的实例来获取 Activity (在 setContentView 之后)然后是 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 为空。

这是 Activity 布局:
<?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>

这是 Activity 的 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 documentation (在 FragmentTransaction 中执行 NavController 的底层 API):

The current Fragment from FragmentNavigator's perspective can be retrieved by calling FragmentManager.getPrimaryNavigationFragment() with the FragmentManager passed to this FragmentNavigator.


val navFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
val mapFragment = navFragment!!.childFragmentManager.getPrimaryNavigationFragment()

关于android - 从 Activity 中获取嵌套在导航 Controller 内的 fragment 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58866930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com