min
09/17/2025, 11:58 AMfun ComponentActivity.mySetContent(
parent: CompositionContext? = null,
content: @Composable () -> Unit,
) {
val existingView = this.window.decorView
.findViewById<ViewGroup>(android.R.id.content)
.getChildAt(0) as? ComposeView
val compositionDest = (existingView ?: ComposeView(this)).also { view ->
view.setParentCompositionContext(parent)
view.setContent(content)
}
if (existingView != compositionDest) {
this.setContentView(compositionDest, ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
))
this.window.decorView.also { decorView ->
if (decorView.findViewTreeLifecycleOwner() == null) {
decorView.setViewTreeLifecycleOwner(this)
}
if (decorView.findViewTreeViewModelStoreOwner() == null) {
decorView.setViewTreeViewModelStoreOwner(this)
}
if (decorView.findViewTreeSavedStateRegistryOwner() == null) {
decorView.setViewTreeSavedStateRegistryOwner(this)
}
}
}
}
I’m reimplementing parts of Jetpack Compose to get a better idea of how it works. My understanding is that existingView
is only not null
if mySetContent
has already been called on the current ComponentActivity
object; that the ComposeView
created in the assignment to compositionDest
gets registered in the if
body via the call to this.setContentView
the first time the method is called; and that it’s this ComposeView
object that every subsequent call to the method on the same instance gets access to via existingView
. Am I correct, and if so, there should be nothing wrong with swapping out ComposeView
for MyComposeView
(my implementation) here, right?Michael Paus
09/17/2025, 12:03 PMZach Klippenstein (he/him) [MOD]
09/17/2025, 5:19 PMmin
09/18/2025, 6:24 AMmin
09/18/2025, 6:24 AMmin
09/18/2025, 6:25 AMmin
09/18/2025, 6:57 AMZach Klippenstein (he/him) [MOD]
09/18/2025, 12:54 PM