https://kotlinlang.org logo
#compose
Title
# compose
c

clhols

02/12/2021, 3:47 PM
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

André Thiele

02/12/2021, 4:02 PM
Will try this in a moment, seems like a similiar error to what I posted
i

Ian Lake

02/12/2021, 4:06 PM
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

clhols

02/15/2021, 9:07 AM
That was it, thanks!
p

philglass

02/15/2021, 1:32 PM
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

Ian Lake

02/15/2021, 3:52 PM
You should never have been doing that
p

philglass

02/15/2021, 3:55 PM
Why’s that?
i

Ian Lake

02/15/2021, 3:57 PM
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

philglass

02/15/2021, 5:06 PM
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

Vitor Prado

02/19/2021, 3:51 PM
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

Thomas

03/25/2021, 10:12 PM
@Vitor Prado did you find a solution? I am experiencing the same issue at the moment.
i

Ian Lake

03/25/2021, 11:54 PM
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

Thomas

03/26/2021, 12:09 AM
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

Ian Lake

03/26/2021, 12:25 AM
Just make sure you're never using just
Activity
- you always need to use
androidx.activity.ComponentActivity
or one of its subclasses
👍 1
3 Views