I am doing this: ```return inflater.inflate(R.layo...
# compose
c
I am doing this:
Copy code
return 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-alpha12
a
Will try this in a moment, seems like a similiar error to what I posted
i
Are you using
AppCompatActivity
? 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.0
c
That was it, thanks!
p
We’re also seeing this after upgrading to alpha 12. We have dependencies on the latest appcompat + activity artifacts. Looking at
ComponentActivity
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?
1
i
You should never have been doing that
p
Why’s that?
i
Well, besides that you should always be using
FragmentContainerView
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 doing
p
I see. I don’t think there’s any particularly good reason we use
android.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
).
v
Event after update appcompat the app crashes with the same error.. my activity is using “setContentView” by databinding util class.. could it be the problem?
t
@Vitor Prado did you find a solution? I am experiencing the same issue at the moment.
i
If you're still having issues in Compose beta03 (which contained a few more fixes around this), please file a bug with a project that we can take a look at: https://issuetracker.google.com/issues/new?component=612128
t
Thanks, I'll look into it (I am using Compose for Desktop so will have to check the versions). It is only happening in the Wear OS version of the app, the same code in the Android phone version is working fine.
i
Just make sure you're never using just
Activity
- you always need to use
androidx.activity.ComponentActivity
or one of its subclasses
👍 1