android - Jetpack Compose - BottomSheetDialogFragment 和 ViewTreeLifecycleOwner 的问题
问题描述
将 Jetpack Compose 库更新为 后beta01
,我无法显示 DialogFragment 或 BottomSheetDialogFragment。看起来ViewTreeLifecycleOwner
在我的活动中找不到了。另外,我尝试了几种可能的解决方案,但都没有成功。
片段中的视图被夸大了:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(
R.layout.fragment_full_screen_layout, container, false
).apply {
findViewById<ComposeView>(R.id.compose_view).setContent {...}
对话框片段显示为:
DialogFragmentExample.newInstance().show(supportFragmentManager, null)
有没有人有类似的问题?请,任何建议都将受到欢迎。
堆栈跟踪:
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from DecorView@409dd5d[MainActivity]
at androidx.compose.ui.platform.WindowRecomposer_androidKt.createLifecycleAwareViewTreeRecomposer(WindowRecomposer.android.kt:214)
at androidx.compose.ui.platform.WindowRecomposer_androidKt.access$createLifecycleAwareViewTreeRecomposer(WindowRecomposer.android.kt:1)
at androidx.compose.ui.platform.WindowRecomposerFactory$Companion$LifecycleAware$1.createRecomposer(WindowRecomposer.android.kt:98)
at androidx.compose.ui.platform.WindowRecomposerPolicy.createAndInstallWindowRecomposer$ui_release(WindowRecomposer.android.kt:151)
at androidx.compose.ui.platform.WindowRecomposer_androidKt.getWindowRecomposer(WindowRecomposer.android.kt:199)
at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.android.kt:177)
at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.android.kt:222)
解决方案
我什至没有使用DialogFragment
.
我的解决方法是设置ViewTreeLifecycleOwner
from Activity
:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ViewTreeLifecycleOwner.set(window.decorView, this)
// setContent(...); add fragment using Compose view
}
}
我目前正在使用以下库:
androidx.activity:activity-ktx:1.3.0-alpha04
androidx.fragment:fragment-ktx:1.3.1
androidx.compose.*:*:1.0.0-beta02
推荐阅读
- twilio - 无法访问本地和远程参与者的 mediaStreamTracks
- r - 使用 := 编辑数据表中日期列的年份
- c - 为什么我的计算器不能用,我用C写的
- mysql - mySQL 在新创建的列上使用 where
- modelica - 分配到连接器
- python - 如何取消透视多列数据?
- latex - 进入小节后的缩进(背面)
- sql - 在 IIF 函数中使用 Select 语句
- python - Finding Eigenvalues with a Variable in the Matrix
- chromecast - 为 Google Cast Styled Media Receiver 设置支持的媒体命令