clhols
02/12/2021, 3:47 PMreturn inflater.inflate(R.layout.fragment_hello, container, false).apply {
findViewById<ComposeView>(R.id.cvHelloWorld).setContent {
// In Compose world
MaterialTheme {
HelloWorld()
}
}
}
But then I get:
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from DecorView@d0eeb9[HomeActivity]
at androidx.compose.ui.platform.WindowRecomposerKt.createLifecycleAwareViewTreeRecomposer(WindowRecomposer.kt:231)
at androidx.compose.ui.platform.WindowRecomposerKt.access$createLifecycleAwareViewTreeRecomposer(WindowRecomposer.kt:1)
at androidx.compose.ui.platform.WindowRecomposerFactory$Companion$LifecycleAware$1.createRecomposer(WindowRecomposer.kt:115)
at androidx.compose.ui.platform.WindowRecomposerPolicy.createAndInstallWindowRecomposer$ui_release(WindowRecomposer.kt:168)
at androidx.compose.ui.platform.WindowRecomposerKt.getWindowRecomposer(WindowRecomposer.kt:216)
at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.kt:184)
at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.kt:215)
at android.view.View.dispatchAttachedToWindow(View.java:17445)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3326)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3333)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3333)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3333)
at android.view.ViewGroup.addViewInner(ViewGroup.java:4977)
at android.view.ViewGroup.addView(ViewGroup.java:4768)
at androidx.fragment.app.FragmentContainerView.addView(FragmentContainerView.java:280)
at android.view.ViewGroup.addView(ViewGroup.java:4708)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:536)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1324)
at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2392)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2137)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2061)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1957)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:496)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I am using Kotlin 1.4.30, Compose 1.0.0-alpha12André Thiele
02/12/2021, 4:02 PMIan Lake
02/12/2021, 4:06 PMAppCompatActivity
? If so, are you using AppCompat 1.3.0-beta01
? Only the 1.3 versions populate the ViewTreeLifecycleOwner
at the decor view level, as per the Activity release notes: https://developer.android.com/jetpack/androidx/releases/activity#1.2.0clhols
02/15/2021, 9:07 AMphilglass
02/15/2021, 1:32 PMComponentActivity
I’m wondering if this is because we never call setContentView
or addContentView
, and so the ViewTreeLifecycleOwner
that compose expects to be there is never set up (we add fragments directly to android.R.id.content
). Does that sound right? If so, is this a bug or is adding stuff directly to android.R.id.content
not a thing we should be doing any more?Ian Lake
02/15/2021, 3:52 PMphilglass
02/15/2021, 3:55 PMIan Lake
02/15/2021, 3:57 PMFragmentContainerView
for hosting fragments (as that's the only way to get the right z ordering of animations and dispatch of window inserts), you're also bypassing the framing and insetting that AppCompatActivity
is also doingphilglass
02/15/2021, 5:06 PMandroid.R.id.content
, I imagine it just worked at the time and hasn’t caused any practical problems until now (we have a navigation abstraction on top of fragments that predates FragmentContainerView
).Vitor Prado
02/19/2021, 3:51 PMThomas
03/25/2021, 10:12 PMIan Lake
03/25/2021, 11:54 PMThomas
03/26/2021, 12:09 AMIan Lake
03/26/2021, 12:25 AMActivity
- you always need to use androidx.activity.ComponentActivity
or one of its subclasses