【发布时间】:2020-11-18 07:32:37
【问题描述】:
我正在尝试扩充包含 FragmentContainerView 的视图,该视图位于要放入布局的片段中。
- 根视图
- 片段
- 自定义视图
- 片段容器视图
- 自定义视图
- 片段
像这样:
val transaction = fragmentManager.beginTransaction()
transaction.replace(R.id.main_content, Frag(), "tag");
transaction.commitNowAllowingStateLoss()
Frag 会创建一个视图,该视图会像这样对 xml 进行膨胀:
<?xml version="1.0" encoding="utf-8"?>
<merge 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:parentTag="android.widget.FrameLayout">
<androidx.fragment.app.FragmentContainerView
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_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
</merge>
但是当我执行代码时,我的应用程序崩溃了:
原因:java.lang.IllegalStateException: FragmentManager 已经在执行事务
使用FragmentContainerView 时如何避免此错误(注意:如果我使用<fragment> 而不是<androidx.fragment.app.FragmentContainerView>,一切正常)
【问题讨论】:
标签: android android-fragments android-navigation android-navigation-graph